ia64/xen-unstable

changeset 6334:6893bc5cc225

Add xenstat framework.
# HG changeset patch
# User josht@us.ibm.com
# Node ID d39cf132a00a91e65c0ad4640c5bd17f722788b7
# Parent 822566f5cfddce3b641b5a1a119dfd592ec69158
Add xenstat framework.
author josht@us.ibm.com
date Fri Aug 12 18:36:54 2005 +0000 (2005-08-12)
parents ba46bd7f5cdf
children 59328ad2a7d4
files tools/Makefile tools/Rules.mk tools/xenstat/Makefile tools/xenstat/libxenstat/COPYING tools/xenstat/libxenstat/Makefile tools/xenstat/libxenstat/bindings/swig/xenstat.i tools/xenstat/libxenstat/src/xen-interface.c tools/xenstat/libxenstat/src/xen-interface.h tools/xenstat/libxenstat/src/xenstat.c tools/xenstat/libxenstat/src/xenstat.h tools/xenstat/vm-top/Makefile tools/xenstat/vm-top/TODO tools/xenstat/vm-top/vm-top.1 tools/xenstat/vm-top/vm-top.c
line diff
     1.1 --- a/tools/Makefile	Fri Aug 12 15:59:34 2005 +0000
     1.2 +++ b/tools/Makefile	Fri Aug 12 18:36:54 2005 +0000
     1.3 @@ -14,6 +14,7 @@ SUBDIRS += xcutils
     1.4  SUBDIRS += firmware
     1.5  SUBDIRS += security
     1.6  SUBDIRS += console
     1.7 +SUBDIRS += xenstat
     1.8  
     1.9  .PHONY: all install clean check check_clean ioemu eioemuinstall ioemuclean
    1.10  
     2.1 --- a/tools/Rules.mk	Fri Aug 12 15:59:34 2005 +0000
     2.2 +++ b/tools/Rules.mk	Fri Aug 12 18:36:54 2005 +0000
     2.3 @@ -6,6 +6,7 @@ XEN_XC             = $(XEN_ROOT)/tools/p
     2.4  XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
     2.5  XEN_XCS            = $(XEN_ROOT)/tools/xcs
     2.6  XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
     2.7 +XEN_LIBXENSTAT     = $(XEN_ROOT)/tools/xenstat/libxenstat/src
     2.8  
     2.9  ifeq ($(XEN_TARGET_ARCH),x86_32)
    2.10  CFLAGS  += -m32 -march=i686
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/xenstat/Makefile	Fri Aug 12 18:36:54 2005 +0000
     3.3 @@ -0,0 +1,13 @@
     3.4 +XEN_ROOT = ../..
     3.5 +include $(XEN_ROOT)/tools/Rules.mk
     3.6 +
     3.7 +SUBDIRS :=
     3.8 +SUBDIRS += libxenstat
     3.9 +SUBDIRS += vm-top
    3.10 +
    3.11 +.PHONY: all install clean
    3.12 +
    3.13 +all install clean:
    3.14 +	@set -e; for subdir in $(SUBDIRS); do \
    3.15 +		$(MAKE) -C $$subdir $@; \
    3.16 +	done
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/xenstat/libxenstat/COPYING	Fri Aug 12 18:36:54 2005 +0000
     4.3 @@ -0,0 +1,510 @@
     4.4 +
     4.5 +                  GNU LESSER GENERAL PUBLIC LICENSE
     4.6 +                       Version 2.1, February 1999
     4.7 +
     4.8 + Copyright (C) 1991, 1999 Free Software Foundation, Inc.
     4.9 +	51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.10 + Everyone is permitted to copy and distribute verbatim copies
    4.11 + of this license document, but changing it is not allowed.
    4.12 +
    4.13 +[This is the first released version of the Lesser GPL.  It also counts
    4.14 + as the successor of the GNU Library Public License, version 2, hence
    4.15 + the version number 2.1.]
    4.16 +
    4.17 +                            Preamble
    4.18 +
    4.19 +  The licenses for most software are designed to take away your
    4.20 +freedom to share and change it.  By contrast, the GNU General Public
    4.21 +Licenses are intended to guarantee your freedom to share and change
    4.22 +free software--to make sure the software is free for all its users.
    4.23 +
    4.24 +  This license, the Lesser General Public License, applies to some
    4.25 +specially designated software packages--typically libraries--of the
    4.26 +Free Software Foundation and other authors who decide to use it.  You
    4.27 +can use it too, but we suggest you first think carefully about whether
    4.28 +this license or the ordinary General Public License is the better
    4.29 +strategy to use in any particular case, based on the explanations
    4.30 +below.
    4.31 +
    4.32 +  When we speak of free software, we are referring to freedom of use,
    4.33 +not price.  Our General Public Licenses are designed to make sure that
    4.34 +you have the freedom to distribute copies of free software (and charge
    4.35 +for this service if you wish); that you receive source code or can get
    4.36 +it if you want it; that you can change the software and use pieces of
    4.37 +it in new free programs; and that you are informed that you can do
    4.38 +these things.
    4.39 +
    4.40 +  To protect your rights, we need to make restrictions that forbid
    4.41 +distributors to deny you these rights or to ask you to surrender these
    4.42 +rights.  These restrictions translate to certain responsibilities for
    4.43 +you if you distribute copies of the library or if you modify it.
    4.44 +
    4.45 +  For example, if you distribute copies of the library, whether gratis
    4.46 +or for a fee, you must give the recipients all the rights that we gave
    4.47 +you.  You must make sure that they, too, receive or can get the source
    4.48 +code.  If you link other code with the library, you must provide
    4.49 +complete object files to the recipients, so that they can relink them
    4.50 +with the library after making changes to the library and recompiling
    4.51 +it.  And you must show them these terms so they know their rights.
    4.52 +
    4.53 +  We protect your rights with a two-step method: (1) we copyright the
    4.54 +library, and (2) we offer you this license, which gives you legal
    4.55 +permission to copy, distribute and/or modify the library.
    4.56 +
    4.57 +  To protect each distributor, we want to make it very clear that
    4.58 +there is no warranty for the free library.  Also, if the library is
    4.59 +modified by someone else and passed on, the recipients should know
    4.60 +that what they have is not the original version, so that the original
    4.61 +author's reputation will not be affected by problems that might be
    4.62 +introduced by others.
    4.63 +
    4.64 +  Finally, software patents pose a constant threat to the existence of
    4.65 +any free program.  We wish to make sure that a company cannot
    4.66 +effectively restrict the users of a free program by obtaining a
    4.67 +restrictive license from a patent holder.  Therefore, we insist that
    4.68 +any patent license obtained for a version of the library must be
    4.69 +consistent with the full freedom of use specified in this license.
    4.70 +
    4.71 +  Most GNU software, including some libraries, is covered by the
    4.72 +ordinary GNU General Public License.  This license, the GNU Lesser
    4.73 +General Public License, applies to certain designated libraries, and
    4.74 +is quite different from the ordinary General Public License.  We use
    4.75 +this license for certain libraries in order to permit linking those
    4.76 +libraries into non-free programs.
    4.77 +
    4.78 +  When a program is linked with a library, whether statically or using
    4.79 +a shared library, the combination of the two is legally speaking a
    4.80 +combined work, a derivative of the original library.  The ordinary
    4.81 +General Public License therefore permits such linking only if the
    4.82 +entire combination fits its criteria of freedom.  The Lesser General
    4.83 +Public License permits more lax criteria for linking other code with
    4.84 +the library.
    4.85 +
    4.86 +  We call this license the "Lesser" General Public License because it
    4.87 +does Less to protect the user's freedom than the ordinary General
    4.88 +Public License.  It also provides other free software developers Less
    4.89 +of an advantage over competing non-free programs.  These disadvantages
    4.90 +are the reason we use the ordinary General Public License for many
    4.91 +libraries.  However, the Lesser license provides advantages in certain
    4.92 +special circumstances.
    4.93 +
    4.94 +  For example, on rare occasions, there may be a special need to
    4.95 +encourage the widest possible use of a certain library, so that it
    4.96 +becomes a de-facto standard.  To achieve this, non-free programs must
    4.97 +be allowed to use the library.  A more frequent case is that a free
    4.98 +library does the same job as widely used non-free libraries.  In this
    4.99 +case, there is little to gain by limiting the free library to free
   4.100 +software only, so we use the Lesser General Public License.
   4.101 +
   4.102 +  In other cases, permission to use a particular library in non-free
   4.103 +programs enables a greater number of people to use a large body of
   4.104 +free software.  For example, permission to use the GNU C Library in
   4.105 +non-free programs enables many more people to use the whole GNU
   4.106 +operating system, as well as its variant, the GNU/Linux operating
   4.107 +system.
   4.108 +
   4.109 +  Although the Lesser General Public License is Less protective of the
   4.110 +users' freedom, it does ensure that the user of a program that is
   4.111 +linked with the Library has the freedom and the wherewithal to run
   4.112 +that program using a modified version of the Library.
   4.113 +
   4.114 +  The precise terms and conditions for copying, distribution and
   4.115 +modification follow.  Pay close attention to the difference between a
   4.116 +"work based on the library" and a "work that uses the library".  The
   4.117 +former contains code derived from the library, whereas the latter must
   4.118 +be combined with the library in order to run.
   4.119 +
   4.120 +                  GNU LESSER GENERAL PUBLIC LICENSE
   4.121 +   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
   4.122 +
   4.123 +  0. This License Agreement applies to any software library or other
   4.124 +program which contains a notice placed by the copyright holder or
   4.125 +other authorized party saying it may be distributed under the terms of
   4.126 +this Lesser General Public License (also called "this License").
   4.127 +Each licensee is addressed as "you".
   4.128 +
   4.129 +  A "library" means a collection of software functions and/or data
   4.130 +prepared so as to be conveniently linked with application programs
   4.131 +(which use some of those functions and data) to form executables.
   4.132 +
   4.133 +  The "Library", below, refers to any such software library or work
   4.134 +which has been distributed under these terms.  A "work based on the
   4.135 +Library" means either the Library or any derivative work under
   4.136 +copyright law: that is to say, a work containing the Library or a
   4.137 +portion of it, either verbatim or with modifications and/or translated
   4.138 +straightforwardly into another language.  (Hereinafter, translation is
   4.139 +included without limitation in the term "modification".)
   4.140 +
   4.141 +  "Source code" for a work means the preferred form of the work for
   4.142 +making modifications to it.  For a library, complete source code means
   4.143 +all the source code for all modules it contains, plus any associated
   4.144 +interface definition files, plus the scripts used to control
   4.145 +compilation and installation of the library.
   4.146 +
   4.147 +  Activities other than copying, distribution and modification are not
   4.148 +covered by this License; they are outside its scope.  The act of
   4.149 +running a program using the Library is not restricted, and output from
   4.150 +such a program is covered only if its contents constitute a work based
   4.151 +on the Library (independent of the use of the Library in a tool for
   4.152 +writing it).  Whether that is true depends on what the Library does
   4.153 +and what the program that uses the Library does.
   4.154 +
   4.155 +  1. You may copy and distribute verbatim copies of the Library's
   4.156 +complete source code as you receive it, in any medium, provided that
   4.157 +you conspicuously and appropriately publish on each copy an
   4.158 +appropriate copyright notice and disclaimer of warranty; keep intact
   4.159 +all the notices that refer to this License and to the absence of any
   4.160 +warranty; and distribute a copy of this License along with the
   4.161 +Library.
   4.162 +
   4.163 +  You may charge a fee for the physical act of transferring a copy,
   4.164 +and you may at your option offer warranty protection in exchange for a
   4.165 +fee.
   4.166 +
   4.167 +  2. You may modify your copy or copies of the Library or any portion
   4.168 +of it, thus forming a work based on the Library, and copy and
   4.169 +distribute such modifications or work under the terms of Section 1
   4.170 +above, provided that you also meet all of these conditions:
   4.171 +
   4.172 +    a) The modified work must itself be a software library.
   4.173 +
   4.174 +    b) You must cause the files modified to carry prominent notices
   4.175 +    stating that you changed the files and the date of any change.
   4.176 +
   4.177 +    c) You must cause the whole of the work to be licensed at no
   4.178 +    charge to all third parties under the terms of this License.
   4.179 +
   4.180 +    d) If a facility in the modified Library refers to a function or a
   4.181 +    table of data to be supplied by an application program that uses
   4.182 +    the facility, other than as an argument passed when the facility
   4.183 +    is invoked, then you must make a good faith effort to ensure that,
   4.184 +    in the event an application does not supply such function or
   4.185 +    table, the facility still operates, and performs whatever part of
   4.186 +    its purpose remains meaningful.
   4.187 +
   4.188 +    (For example, a function in a library to compute square roots has
   4.189 +    a purpose that is entirely well-defined independent of the
   4.190 +    application.  Therefore, Subsection 2d requires that any
   4.191 +    application-supplied function or table used by this function must
   4.192 +    be optional: if the application does not supply it, the square
   4.193 +    root function must still compute square roots.)
   4.194 +
   4.195 +These requirements apply to the modified work as a whole.  If
   4.196 +identifiable sections of that work are not derived from the Library,
   4.197 +and can be reasonably considered independent and separate works in
   4.198 +themselves, then this License, and its terms, do not apply to those
   4.199 +sections when you distribute them as separate works.  But when you
   4.200 +distribute the same sections as part of a whole which is a work based
   4.201 +on the Library, the distribution of the whole must be on the terms of
   4.202 +this License, whose permissions for other licensees extend to the
   4.203 +entire whole, and thus to each and every part regardless of who wrote
   4.204 +it.
   4.205 +
   4.206 +Thus, it is not the intent of this section to claim rights or contest
   4.207 +your rights to work written entirely by you; rather, the intent is to
   4.208 +exercise the right to control the distribution of derivative or
   4.209 +collective works based on the Library.
   4.210 +
   4.211 +In addition, mere aggregation of another work not based on the Library
   4.212 +with the Library (or with a work based on the Library) on a volume of
   4.213 +a storage or distribution medium does not bring the other work under
   4.214 +the scope of this License.
   4.215 +
   4.216 +  3. You may opt to apply the terms of the ordinary GNU General Public
   4.217 +License instead of this License to a given copy of the Library.  To do
   4.218 +this, you must alter all the notices that refer to this License, so
   4.219 +that they refer to the ordinary GNU General Public License, version 2,
   4.220 +instead of to this License.  (If a newer version than version 2 of the
   4.221 +ordinary GNU General Public License has appeared, then you can specify
   4.222 +that version instead if you wish.)  Do not make any other change in
   4.223 +these notices.
   4.224 +
   4.225 +  Once this change is made in a given copy, it is irreversible for
   4.226 +that copy, so the ordinary GNU General Public License applies to all
   4.227 +subsequent copies and derivative works made from that copy.
   4.228 +
   4.229 +  This option is useful when you wish to copy part of the code of
   4.230 +the Library into a program that is not a library.
   4.231 +
   4.232 +  4. You may copy and distribute the Library (or a portion or
   4.233 +derivative of it, under Section 2) in object code or executable form
   4.234 +under the terms of Sections 1 and 2 above provided that you accompany
   4.235 +it with the complete corresponding machine-readable source code, which
   4.236 +must be distributed under the terms of Sections 1 and 2 above on a
   4.237 +medium customarily used for software interchange.
   4.238 +
   4.239 +  If distribution of object code is made by offering access to copy
   4.240 +from a designated place, then offering equivalent access to copy the
   4.241 +source code from the same place satisfies the requirement to
   4.242 +distribute the source code, even though third parties are not
   4.243 +compelled to copy the source along with the object code.
   4.244 +
   4.245 +  5. A program that contains no derivative of any portion of the
   4.246 +Library, but is designed to work with the Library by being compiled or
   4.247 +linked with it, is called a "work that uses the Library".  Such a
   4.248 +work, in isolation, is not a derivative work of the Library, and
   4.249 +therefore falls outside the scope of this License.
   4.250 +
   4.251 +  However, linking a "work that uses the Library" with the Library
   4.252 +creates an executable that is a derivative of the Library (because it
   4.253 +contains portions of the Library), rather than a "work that uses the
   4.254 +library".  The executable is therefore covered by this License.
   4.255 +Section 6 states terms for distribution of such executables.
   4.256 +
   4.257 +  When a "work that uses the Library" uses material from a header file
   4.258 +that is part of the Library, the object code for the work may be a
   4.259 +derivative work of the Library even though the source code is not.
   4.260 +Whether this is true is especially significant if the work can be
   4.261 +linked without the Library, or if the work is itself a library.  The
   4.262 +threshold for this to be true is not precisely defined by law.
   4.263 +
   4.264 +  If such an object file uses only numerical parameters, data
   4.265 +structure layouts and accessors, and small macros and small inline
   4.266 +functions (ten lines or less in length), then the use of the object
   4.267 +file is unrestricted, regardless of whether it is legally a derivative
   4.268 +work.  (Executables containing this object code plus portions of the
   4.269 +Library will still fall under Section 6.)
   4.270 +
   4.271 +  Otherwise, if the work is a derivative of the Library, you may
   4.272 +distribute the object code for the work under the terms of Section 6.
   4.273 +Any executables containing that work also fall under Section 6,
   4.274 +whether or not they are linked directly with the Library itself.
   4.275 +
   4.276 +  6. As an exception to the Sections above, you may also combine or
   4.277 +link a "work that uses the Library" with the Library to produce a
   4.278 +work containing portions of the Library, and distribute that work
   4.279 +under terms of your choice, provided that the terms permit
   4.280 +modification of the work for the customer's own use and reverse
   4.281 +engineering for debugging such modifications.
   4.282 +
   4.283 +  You must give prominent notice with each copy of the work that the
   4.284 +Library is used in it and that the Library and its use are covered by
   4.285 +this License.  You must supply a copy of this License.  If the work
   4.286 +during execution displays copyright notices, you must include the
   4.287 +copyright notice for the Library among them, as well as a reference
   4.288 +directing the user to the copy of this License.  Also, you must do one
   4.289 +of these things:
   4.290 +
   4.291 +    a) Accompany the work with the complete corresponding
   4.292 +    machine-readable source code for the Library including whatever
   4.293 +    changes were used in the work (which must be distributed under
   4.294 +    Sections 1 and 2 above); and, if the work is an executable linked
   4.295 +    with the Library, with the complete machine-readable "work that
   4.296 +    uses the Library", as object code and/or source code, so that the
   4.297 +    user can modify the Library and then relink to produce a modified
   4.298 +    executable containing the modified Library.  (It is understood
   4.299 +    that the user who changes the contents of definitions files in the
   4.300 +    Library will not necessarily be able to recompile the application
   4.301 +    to use the modified definitions.)
   4.302 +
   4.303 +    b) Use a suitable shared library mechanism for linking with the
   4.304 +    Library.  A suitable mechanism is one that (1) uses at run time a
   4.305 +    copy of the library already present on the user's computer system,
   4.306 +    rather than copying library functions into the executable, and (2)
   4.307 +    will operate properly with a modified version of the library, if
   4.308 +    the user installs one, as long as the modified version is
   4.309 +    interface-compatible with the version that the work was made with.
   4.310 +
   4.311 +    c) Accompany the work with a written offer, valid for at least
   4.312 +    three years, to give the same user the materials specified in
   4.313 +    Subsection 6a, above, for a charge no more than the cost of
   4.314 +    performing this distribution.
   4.315 +
   4.316 +    d) If distribution of the work is made by offering access to copy
   4.317 +    from a designated place, offer equivalent access to copy the above
   4.318 +    specified materials from the same place.
   4.319 +
   4.320 +    e) Verify that the user has already received a copy of these
   4.321 +    materials or that you have already sent this user a copy.
   4.322 +
   4.323 +  For an executable, the required form of the "work that uses the
   4.324 +Library" must include any data and utility programs needed for
   4.325 +reproducing the executable from it.  However, as a special exception,
   4.326 +the materials to be distributed need not include anything that is
   4.327 +normally distributed (in either source or binary form) with the major
   4.328 +components (compiler, kernel, and so on) of the operating system on
   4.329 +which the executable runs, unless that component itself accompanies
   4.330 +the executable.
   4.331 +
   4.332 +  It may happen that this requirement contradicts the license
   4.333 +restrictions of other proprietary libraries that do not normally
   4.334 +accompany the operating system.  Such a contradiction means you cannot
   4.335 +use both them and the Library together in an executable that you
   4.336 +distribute.
   4.337 +
   4.338 +  7. You may place library facilities that are a work based on the
   4.339 +Library side-by-side in a single library together with other library
   4.340 +facilities not covered by this License, and distribute such a combined
   4.341 +library, provided that the separate distribution of the work based on
   4.342 +the Library and of the other library facilities is otherwise
   4.343 +permitted, and provided that you do these two things:
   4.344 +
   4.345 +    a) Accompany the combined library with a copy of the same work
   4.346 +    based on the Library, uncombined with any other library
   4.347 +    facilities.  This must be distributed under the terms of the
   4.348 +    Sections above.
   4.349 +
   4.350 +    b) Give prominent notice with the combined library of the fact
   4.351 +    that part of it is a work based on the Library, and explaining
   4.352 +    where to find the accompanying uncombined form of the same work.
   4.353 +
   4.354 +  8. You may not copy, modify, sublicense, link with, or distribute
   4.355 +the Library except as expressly provided under this License.  Any
   4.356 +attempt otherwise to copy, modify, sublicense, link with, or
   4.357 +distribute the Library is void, and will automatically terminate your
   4.358 +rights under this License.  However, parties who have received copies,
   4.359 +or rights, from you under this License will not have their licenses
   4.360 +terminated so long as such parties remain in full compliance.
   4.361 +
   4.362 +  9. You are not required to accept this License, since you have not
   4.363 +signed it.  However, nothing else grants you permission to modify or
   4.364 +distribute the Library or its derivative works.  These actions are
   4.365 +prohibited by law if you do not accept this License.  Therefore, by
   4.366 +modifying or distributing the Library (or any work based on the
   4.367 +Library), you indicate your acceptance of this License to do so, and
   4.368 +all its terms and conditions for copying, distributing or modifying
   4.369 +the Library or works based on it.
   4.370 +
   4.371 +  10. Each time you redistribute the Library (or any work based on the
   4.372 +Library), the recipient automatically receives a license from the
   4.373 +original licensor to copy, distribute, link with or modify the Library
   4.374 +subject to these terms and conditions.  You may not impose any further
   4.375 +restrictions on the recipients' exercise of the rights granted herein.
   4.376 +You are not responsible for enforcing compliance by third parties with
   4.377 +this License.
   4.378 +
   4.379 +  11. If, as a consequence of a court judgment or allegation of patent
   4.380 +infringement or for any other reason (not limited to patent issues),
   4.381 +conditions are imposed on you (whether by court order, agreement or
   4.382 +otherwise) that contradict the conditions of this License, they do not
   4.383 +excuse you from the conditions of this License.  If you cannot
   4.384 +distribute so as to satisfy simultaneously your obligations under this
   4.385 +License and any other pertinent obligations, then as a consequence you
   4.386 +may not distribute the Library at all.  For example, if a patent
   4.387 +license would not permit royalty-free redistribution of the Library by
   4.388 +all those who receive copies directly or indirectly through you, then
   4.389 +the only way you could satisfy both it and this License would be to
   4.390 +refrain entirely from distribution of the Library.
   4.391 +
   4.392 +If any portion of this section is held invalid or unenforceable under
   4.393 +any particular circumstance, the balance of the section is intended to
   4.394 +apply, and the section as a whole is intended to apply in other
   4.395 +circumstances.
   4.396 +
   4.397 +It is not the purpose of this section to induce you to infringe any
   4.398 +patents or other property right claims or to contest validity of any
   4.399 +such claims; this section has the sole purpose of protecting the
   4.400 +integrity of the free software distribution system which is
   4.401 +implemented by public license practices.  Many people have made
   4.402 +generous contributions to the wide range of software distributed
   4.403 +through that system in reliance on consistent application of that
   4.404 +system; it is up to the author/donor to decide if he or she is willing
   4.405 +to distribute software through any other system and a licensee cannot
   4.406 +impose that choice.
   4.407 +
   4.408 +This section is intended to make thoroughly clear what is believed to
   4.409 +be a consequence of the rest of this License.
   4.410 +
   4.411 +  12. If the distribution and/or use of the Library is restricted in
   4.412 +certain countries either by patents or by copyrighted interfaces, the
   4.413 +original copyright holder who places the Library under this License
   4.414 +may add an explicit geographical distribution limitation excluding those
   4.415 +countries, so that distribution is permitted only in or among
   4.416 +countries not thus excluded.  In such case, this License incorporates
   4.417 +the limitation as if written in the body of this License.
   4.418 +
   4.419 +  13. The Free Software Foundation may publish revised and/or new
   4.420 +versions of the Lesser General Public License from time to time.
   4.421 +Such new versions will be similar in spirit to the present version,
   4.422 +but may differ in detail to address new problems or concerns.
   4.423 +
   4.424 +Each version is given a distinguishing version number.  If the Library
   4.425 +specifies a version number of this License which applies to it and
   4.426 +"any later version", you have the option of following the terms and
   4.427 +conditions either of that version or of any later version published by
   4.428 +the Free Software Foundation.  If the Library does not specify a
   4.429 +license version number, you may choose any version ever published by
   4.430 +the Free Software Foundation.
   4.431 +
   4.432 +  14. If you wish to incorporate parts of the Library into other free
   4.433 +programs whose distribution conditions are incompatible with these,
   4.434 +write to the author to ask for permission.  For software which is
   4.435 +copyrighted by the Free Software Foundation, write to the Free
   4.436 +Software Foundation; we sometimes make exceptions for this.  Our
   4.437 +decision will be guided by the two goals of preserving the free status
   4.438 +of all derivatives of our free software and of promoting the sharing
   4.439 +and reuse of software generally.
   4.440 +
   4.441 +                            NO WARRANTY
   4.442 +
   4.443 +  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
   4.444 +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
   4.445 +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
   4.446 +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
   4.447 +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
   4.448 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   4.449 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
   4.450 +LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
   4.451 +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
   4.452 +
   4.453 +  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
   4.454 +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
   4.455 +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
   4.456 +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
   4.457 +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
   4.458 +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
   4.459 +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
   4.460 +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
   4.461 +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
   4.462 +DAMAGES.
   4.463 +
   4.464 +                     END OF TERMS AND CONDITIONS
   4.465 +
   4.466 +           How to Apply These Terms to Your New Libraries
   4.467 +
   4.468 +  If you develop a new library, and you want it to be of the greatest
   4.469 +possible use to the public, we recommend making it free software that
   4.470 +everyone can redistribute and change.  You can do so by permitting
   4.471 +redistribution under these terms (or, alternatively, under the terms
   4.472 +of the ordinary General Public License).
   4.473 +
   4.474 +  To apply these terms, attach the following notices to the library.
   4.475 +It is safest to attach them to the start of each source file to most
   4.476 +effectively convey the exclusion of warranty; and each file should
   4.477 +have at least the "copyright" line and a pointer to where the full
   4.478 +notice is found.
   4.479 +
   4.480 +
   4.481 +    <one line to give the library's name and a brief idea of what it does.>
   4.482 +    Copyright (C) <year>  <name of author>
   4.483 +
   4.484 +    This library is free software; you can redistribute it and/or
   4.485 +    modify it under the terms of the GNU Lesser General Public
   4.486 +    License as published by the Free Software Foundation; either
   4.487 +    version 2.1 of the License, or (at your option) any later version.
   4.488 +
   4.489 +    This library is distributed in the hope that it will be useful,
   4.490 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   4.491 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   4.492 +    Lesser General Public License for more details.
   4.493 +
   4.494 +    You should have received a copy of the GNU Lesser General Public
   4.495 +    License along with this library; if not, write to the Free Software
   4.496 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   4.497 +
   4.498 +Also add information on how to contact you by electronic and paper mail.
   4.499 +
   4.500 +You should also get your employer (if you work as a programmer) or
   4.501 +your school, if any, to sign a "copyright disclaimer" for the library,
   4.502 +if necessary.  Here is a sample; alter the names:
   4.503 +
   4.504 +  Yoyodyne, Inc., hereby disclaims all copyright interest in the
   4.505 +  library `Frob' (a library for tweaking knobs) written by James
   4.506 +  Random Hacker.
   4.507 +
   4.508 +  <signature of Ty Coon>, 1 April 1990
   4.509 +  Ty Coon, President of Vice
   4.510 +
   4.511 +That's all there is to it!
   4.512 +
   4.513 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tools/xenstat/libxenstat/Makefile	Fri Aug 12 18:36:54 2005 +0000
     5.3 @@ -0,0 +1,120 @@
     5.4 +# libxenstat: statistics-collection library for Xen
     5.5 +# Copyright (C) International Business Machines Corp., 2005
     5.6 +# Author: Josh Triplett <josht@us.ibm.com>
     5.7 +# 
     5.8 +# This library is free software; you can redistribute it and/or
     5.9 +# modify it under the terms of the GNU Lesser General Public
    5.10 +# License as published by the Free Software Foundation; either
    5.11 +# version 2.1 of the License, or (at your option) any later version.
    5.12 +# 
    5.13 +# This library is distributed in the hope that it will be useful,
    5.14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.16 +# Lesser General Public License for more details.
    5.17 +
    5.18 +XEN_ROOT=../../..
    5.19 +include $(XEN_ROOT)/tools/Rules.mk
    5.20 +LINUX_ROOT := $(XEN_ROOT)/linux-2.6-xen-sparse
    5.21 +
    5.22 +INSTALL         = install
    5.23 +INSTALL_PROG    = $(INSTALL) -m0755 -D
    5.24 +INSTALL_DATA    = $(INSTALL) -m0644 -D
    5.25 +
    5.26 +prefix=/usr
    5.27 +includedir=$(prefix)/include
    5.28 +libdir=$(prefix)/lib
    5.29 +
    5.30 +LDCONFIG=ldconfig
    5.31 +MAKE_LINK=ln -sf
    5.32 +
    5.33 +MAJOR=0
    5.34 +MINOR=0
    5.35 +
    5.36 +LIB=src/libxenstat.so.$(MAJOR).$(MINOR)
    5.37 +LINKS=src/libxenstat.so.$(MAJOR) src/libxenstat.so
    5.38 +OBJECTS=src/xenstat.o src/xen-interface.o
    5.39 +SONAME_FLAGS=-Wl,-soname -Wl,libxenstat.so.$(MAJOR)
    5.40 +
    5.41 +WARN_FLAGS=-Wall -Werror
    5.42 +
    5.43 +CFLAGS+=-Isrc
    5.44 +CFLAGS+=-I$(XEN_ROOT)/xen/include/public
    5.45 +CFLAGS+=-I$(LINUX_ROOT)/include/asm-xen/linux-public/
    5.46 +LDFLAGS+=-Lsrc
    5.47 +
    5.48 +# Note that the bindings are not built by default, because they require a
    5.49 +# number of external dependencies.
    5.50 +all: $(LIB) $(LINKS)
    5.51 +
    5.52 +$(LIB): $(OBJECTS)
    5.53 +	$(CC) $(LDFLAGS) $(SONAME_FLAGS) -shared -o $@ $(OBJECTS)
    5.54 +
    5.55 +src/xenstat.o: src/xenstat.c src/xenstat.h src/xen-interface.h
    5.56 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
    5.57 +
    5.58 +src/xen-interface.o: src/xen-interface.c src/xen-interface.h
    5.59 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
    5.60 +
    5.61 +src/libxenstat.so.$(MAJOR): $(LIB)
    5.62 +	$(MAKE_LINK) $(<F) $@
    5.63 +
    5.64 +src/libxenstat.so: src/libxenstat.so.$(MAJOR)
    5.65 +	$(MAKE_LINK) $(<F) $@
    5.66 +
    5.67 +install: all
    5.68 +	$(INSTALL_DATA) src/xenstat.h $(DESTDIR)$(includedir)/xenstat.h
    5.69 +	$(INSTALL_PROG) $(LIB) \
    5.70 +	                $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR).$(MINOR)
    5.71 +	$(MAKE_LINK) libxenstat.so.$(MAJOR).$(MINOR) \
    5.72 +	             $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR)
    5.73 +	$(MAKE_LINK) libxenstat.so.$(MAJOR) \
    5.74 +	             $(DESTDIR)$(libdir)/libxenstat.so
    5.75 +	-$(LDCONFIG)
    5.76 +
    5.77 +PYLIB=bindings/swig/python/_xenstat.so
    5.78 +PYMOD=bindings/swig/python/xenstat.py
    5.79 +PYSRC=bindings/swig/python/_xenstat.c
    5.80 +PERLLIB=bindings/swig/perl/xenstat.so
    5.81 +PERLMOD=bindings/swig/perl/xenstat.pm
    5.82 +PERLSRC=bindings/swig/perl/xenstat.c
    5.83 +BINDINGS=$(PYLIB) $(PYMOD) $(PERLLIB) $(PERLMOD)
    5.84 +BINDINGSRC=$(PYSRC) $(PERLSRC)
    5.85 +
    5.86 +# The all-bindings target builds all the language bindings
    5.87 +all-bindings: $(BINDINGS)
    5.88 +
    5.89 +$(BINDINGS): $(LIB) $(LINKS) src/xenstat.h
    5.90 +
    5.91 +SWIG_FLAGS=-module xenstat -Isrc
    5.92 +
    5.93 +PYTHON_VERSION=2.3
    5.94 +PYTHON_FLAGS=-I/usr/include/python$(PYTHON_VERSION) -lpython$(PYTHON_VERSION)
    5.95 +$(PYSRC) $(PYMOD): bindings/swig/xenstat.i
    5.96 +	swig -python $(SWIG_FLAGS) -outdir $(@D) -o $(PYSRC) $<
    5.97 +
    5.98 +$(PYLIB): $(PYSRC)
    5.99 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PYTHON_FLAGS) -shared -lxenstat -o $@ $<
   5.100 +
   5.101 +PERL_FLAGS=`perl -MConfig -e 'print "$$Config{ccflags} -I$$Config{archlib}/CORE";'`
   5.102 +$(PERLSRC) $(PERLMOD): bindings/swig/xenstat.i
   5.103 +	swig -perl $(SWIG_FLAGS) -outdir $(@D) -o $(PERLSRC) $<
   5.104 +
   5.105 +$(PERLLIB): $(PERLSRC)
   5.106 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PERL_FLAGS) -shared -lxenstat -o $@ $<
   5.107 +
   5.108 +# The install-bindings target installs all the language bindings
   5.109 +install-bindings: install-perl-bindings install-python-bindings
   5.110 +
   5.111 +pythonlibdir=$(prefix)/lib/python$(PYTHON_VERSION)/site-packages
   5.112 +install-python-bindings: $(PYLIB) $(PYMOD)
   5.113 +	$(INSTALL_PROG) $(PYLIB) $(pythonlibdir)/_xenstat.so
   5.114 +	$(INSTALL_PROG) $(PYMOD) $(pythonlibdir)/xenstat.py
   5.115 +
   5.116 +perllibdir=$(prefix)/lib/site_perl
   5.117 +perlmoddir=$(prefix)/lib/site_perl
   5.118 +install-perl-bindings: $(PERLLIB) $(PERLMOD)
   5.119 +	$(INSTALL_PROG) $(PERLLIB) $(perllibdir)/xenstat.so
   5.120 +	$(INSTALL_PROG) $(PERLMOD) $(perlmoddir)/xenstat.pm
   5.121 +
   5.122 +clean:
   5.123 +	rm -f $(LIB) $(OBJECTS) $(LINKS) $(BINDINGS) $(BINDINGSRC)
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tools/xenstat/libxenstat/bindings/swig/xenstat.i	Fri Aug 12 18:36:54 2005 +0000
     6.3 @@ -0,0 +1,8 @@
     6.4 +%module xenstat_swig
     6.5 +%{
     6.6 +/* Includes the header in the wrapper code */
     6.7 +#include "xenstat.h"
     6.8 +%}
     6.9 +
    6.10 +/* Parse the header file to generate wrappers */
    6.11 +%include "xenstat.h"
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.c	Fri Aug 12 18:36:54 2005 +0000
     7.3 @@ -0,0 +1,144 @@
     7.4 +/* xen-interface.c
     7.5 + *
     7.6 + * Copyright (C) International Business Machines Corp., 2005
     7.7 + * Authors: Josh Triplett <josht@us.ibm.com>
     7.8 + *          Judy Fischbach <jfisch@us.ibm.com>
     7.9 + *
    7.10 + * This library is free software; you can redistribute it and/or
    7.11 + * modify it under the terms of the GNU Lesser General Public
    7.12 + * License as published by the Free Software Foundation; either
    7.13 + * version 2.1 of the License, or (at your option) any later version.
    7.14 + *
    7.15 + * This library is distributed in the hope that it will be useful,
    7.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
    7.18 + * Lesser General Public License for more details.
    7.19 + */
    7.20 +
    7.21 +#include "xen-interface.h"
    7.22 +#include <fcntl.h>
    7.23 +#include <sys/ioctl.h>
    7.24 +#include <sys/mman.h>
    7.25 +#include <stdio.h>
    7.26 +#include <stdlib.h>
    7.27 +#include <unistd.h>
    7.28 +#include "privcmd.h"
    7.29 +#include "xen.h"
    7.30 +
    7.31 +struct xi_handle {
    7.32 +	int fd;
    7.33 +};
    7.34 +
    7.35 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
    7.36 + * calls to the xen-interface functions or NULL if an error occurs. */
    7.37 +xi_handle *xi_init()
    7.38 +{
    7.39 +	xi_handle *handle;
    7.40 +
    7.41 +	handle = (xi_handle *)calloc(1, sizeof(xi_handle));
    7.42 +	if (handle == NULL)
    7.43 +		return NULL;
    7.44 +
    7.45 +	handle->fd = open("/proc/xen/privcmd", O_RDWR);
    7.46 +	if (handle->fd < 0) {
    7.47 +		perror("Couldn't open /proc/xen/privcmd");
    7.48 +		free(handle);
    7.49 +		return NULL;
    7.50 +	}
    7.51 +
    7.52 +	return handle;
    7.53 +}
    7.54 +
    7.55 +/* Release the handle to libxc, free resources, etc. */
    7.56 +void xi_uninit(xi_handle *handle)
    7.57 +{
    7.58 +	close (handle->fd);
    7.59 +	free (handle);
    7.60 +}
    7.61 +
    7.62 +/* Make Xen hypervisor call */
    7.63 +int xi_make_dom0_op(xi_handle *handle, dom0_op_t *op, int opcode)
    7.64 +{
    7.65 +	privcmd_hypercall_t privcmd;
    7.66 +	int ret = 0;
    7.67 +
    7.68 +	/* set up for doing hypercall */
    7.69 +	privcmd.op = __HYPERVISOR_dom0_op;
    7.70 +	privcmd.arg[0] = (unsigned long)op;
    7.71 +	op->cmd = opcode;
    7.72 +	op->interface_version = DOM0_INTERFACE_VERSION;
    7.73 +
    7.74 +	if (mlock( &privcmd, sizeof(privcmd_hypercall_t)) < 0) {
    7.75 +		perror("Failed to mlock privcmd structure");
    7.76 +		return -1;
    7.77 +	}
    7.78 +
    7.79 +	if (mlock( op, sizeof(dom0_op_t)) < 0) {
    7.80 +		perror("Failed to mlock dom0_op structure");
    7.81 +		munlock( &privcmd, sizeof(privcmd_hypercall_t));
    7.82 +		return -1;
    7.83 +	}
    7.84 +
    7.85 +	if (ioctl( handle->fd, IOCTL_PRIVCMD_HYPERCALL, &privcmd) < 0) {
    7.86 +		perror("Hypercall failed");
    7.87 +		ret = -1;
    7.88 +	}
    7.89 +
    7.90 +	munlock( &privcmd, sizeof(privcmd_hypercall_t));
    7.91 +	munlock( op, sizeof(dom0_op_t));
    7.92 +
    7.93 +	return ret;
    7.94 +}
    7.95 +
    7.96 +/* Obtain domain data from dom0 */
    7.97 +int xi_get_physinfo(xi_handle *handle, dom0_physinfo_t *physinfo)
    7.98 +{
    7.99 +	dom0_op_t op;
   7.100 +
   7.101 +	if (xi_make_dom0_op(handle, &op, DOM0_PHYSINFO) < 0) {
   7.102 +		perror("DOM0_PHYSINFO Hypercall failed");
   7.103 +		return -1;
   7.104 +	}
   7.105 +
   7.106 +	*physinfo = op.u.physinfo;
   7.107 +	return 0;
   7.108 +}
   7.109 +
   7.110 +/* Obtain domain data from dom0 */
   7.111 +int xi_get_domaininfolist(xi_handle *handle, dom0_getdomaininfo_t *info,
   7.112 +                          unsigned int first_domain, unsigned int max_domains)
   7.113 +{
   7.114 +	dom0_op_t op;
   7.115 +	op.u.getdomaininfolist.first_domain = first_domain;
   7.116 +	op.u.getdomaininfolist.max_domains = max_domains;
   7.117 +	op.u.getdomaininfolist.buffer = info;
   7.118 +
   7.119 +	if (mlock( info, max_domains * sizeof(dom0_getdomaininfo_t)) < 0) {
   7.120 +		perror("Failed to mlock domaininfo array");
   7.121 +		return -1;
   7.122 +	}
   7.123 +
   7.124 +	if (xi_make_dom0_op(handle, &op, DOM0_GETDOMAININFOLIST) < 0) {
   7.125 +		perror("DOM0_GETDOMAININFOLIST Hypercall failed");
   7.126 +		return -1;
   7.127 +	}
   7.128 +
   7.129 +	return op.u.getdomaininfolist.num_domains;
   7.130 +}
   7.131 +
   7.132 +/* Returns cpu usage data from dom0 */
   7.133 +long long xi_get_vcpu_usage(xi_handle *handle, unsigned int domain,
   7.134 +                            unsigned int vcpu)
   7.135 +{
   7.136 +	dom0_op_t op;
   7.137 +	op.u.getvcpucontext.domain = domain;
   7.138 +	op.u.getvcpucontext.vcpu = vcpu;
   7.139 +	op.u.getvcpucontext.ctxt = NULL;
   7.140 +
   7.141 +	if (xi_make_dom0_op(handle, &op, DOM0_GETVCPUCONTEXT) < 0) {
   7.142 +		perror("DOM0_GETVCPUCONTEXT Hypercall failed");
   7.143 +		return -1;
   7.144 +	}
   7.145 +
   7.146 +	return op.u.getvcpucontext.cpu_time;
   7.147 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.h	Fri Aug 12 18:36:54 2005 +0000
     8.3 @@ -0,0 +1,49 @@
     8.4 +/* xen-interface.h
     8.5 + *
     8.6 + * Copyright (C) International Business Machines Corp., 2005
     8.7 + * Authors: Josh Triplett <josht@us.ibm.com>
     8.8 + *          Judy Fischbach <jfisch@us.ibm.com>
     8.9 + *
    8.10 + * This library is free software; you can redistribute it and/or
    8.11 + * modify it under the terms of the GNU Lesser General Public
    8.12 + * License as published by the Free Software Foundation; either
    8.13 + * version 2.1 of the License, or (at your option) any later version.
    8.14 + *
    8.15 + * This library is distributed in the hope that it will be useful,
    8.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    8.18 + * Lesser General Public License for more details.
    8.19 + */
    8.20 +
    8.21 +#include <stdint.h>
    8.22 +
    8.23 +typedef int8_t   s8;
    8.24 +typedef int16_t  s16;
    8.25 +typedef int32_t  s32;
    8.26 +typedef int64_t  s64;
    8.27 +typedef uint8_t  u8;
    8.28 +typedef uint16_t u16;
    8.29 +typedef uint32_t u32;
    8.30 +typedef uint64_t u64;
    8.31 +
    8.32 +#include "dom0_ops.h"
    8.33 +
    8.34 +/* Opaque handles */
    8.35 +typedef struct xi_handle xi_handle;
    8.36 +
    8.37 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
    8.38 + * calls to the xen-interface functions or NULL if an error occurs. */
    8.39 +xi_handle *xi_init();
    8.40 +
    8.41 +/* Release the handle to libxc, free resources, etc. */
    8.42 +void xi_uninit(xi_handle *handle);
    8.43 +
    8.44 +/* Obtain physinfo data from dom0 */
    8.45 +int xi_get_physinfo(xi_handle *, dom0_physinfo_t *);
    8.46 +
    8.47 +/* Obtain domain data from dom0 */
    8.48 +int xi_get_domaininfolist(xi_handle *, dom0_getdomaininfo_t *, unsigned int,
    8.49 +                          unsigned int);
    8.50 +
    8.51 +/* Returns cpu usage data from dom0 */
    8.52 +long long xi_get_vcpu_usage(xi_handle *, unsigned int, unsigned int);
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/tools/xenstat/libxenstat/src/xenstat.c	Fri Aug 12 18:36:54 2005 +0000
     9.3 @@ -0,0 +1,620 @@
     9.4 +/* libxenstat: statistics-collection library for Xen
     9.5 + * Copyright (C) International Business Machines Corp., 2005
     9.6 + * Authors: Josh Triplett <josht@us.ibm.com>
     9.7 + *          Judy Fischbach <jfisch@us.ibm.com>
     9.8 + *          David Hendricks <dhendrix@us.ibm.com>
     9.9 + *
    9.10 + * This library is free software; you can redistribute it and/or
    9.11 + * modify it under the terms of the GNU Lesser General Public
    9.12 + * License as published by the Free Software Foundation; either
    9.13 + * version 2.1 of the License, or (at your option) any later version.
    9.14 + *
    9.15 + * This library is distributed in the hope that it will be useful,
    9.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    9.18 + * Lesser General Public License for more details.
    9.19 + */
    9.20 +
    9.21 +#include <limits.h>
    9.22 +#include <stdlib.h>
    9.23 +#include <stdio.h>
    9.24 +#include <string.h>
    9.25 +#include <unistd.h>
    9.26 +#include <xen-interface.h>
    9.27 +#include "xenstat.h"
    9.28 +
    9.29 +/*
    9.30 + * Types
    9.31 + */
    9.32 +struct xenstat_handle {
    9.33 +	xi_handle *xihandle;
    9.34 +	int page_size;
    9.35 +	FILE *procnetdev;
    9.36 +};
    9.37 +
    9.38 +struct xenstat_node {
    9.39 +	unsigned int flags;
    9.40 +	unsigned long long cpu_hz;
    9.41 +	unsigned int num_cpus;
    9.42 +	unsigned long long tot_mem;
    9.43 +	unsigned long long free_mem;
    9.44 +	unsigned int num_domains;
    9.45 +	xenstat_domain *domains;	/* Array of length num_domains */
    9.46 +};
    9.47 +
    9.48 +struct xenstat_domain {
    9.49 +	unsigned int id;
    9.50 +	unsigned int state;
    9.51 +	unsigned long long cpu_ns;
    9.52 +	unsigned int num_vcpus;
    9.53 +	xenstat_vcpu *vcpus;	/* Array of length num_vcpus */
    9.54 +	unsigned long long cur_mem;	/* Current memory reservation */
    9.55 +	unsigned long long max_mem;	/* Total memory allowed */
    9.56 +	unsigned int ssid;
    9.57 +	unsigned int num_networks;
    9.58 +	xenstat_network *networks;	/* Array of length num_networks */
    9.59 +};
    9.60 +
    9.61 +struct xenstat_vcpu {
    9.62 +	unsigned long long ns;
    9.63 +};
    9.64 +
    9.65 +struct xenstat_network {
    9.66 +	unsigned int id;
    9.67 +	/* Received */
    9.68 +	unsigned long long rbytes;
    9.69 +	unsigned long long rpackets;
    9.70 +	unsigned long long rerrs;
    9.71 +	unsigned long long rdrop;
    9.72 +	/* Transmitted */
    9.73 +	unsigned long long tbytes;
    9.74 +	unsigned long long tpackets;
    9.75 +	unsigned long long terrs;
    9.76 +	unsigned long long tdrop;
    9.77 +};
    9.78 +
    9.79 +/*
    9.80 + * Data-collection types
    9.81 + */
    9.82 +/* Called to collect the information for the node and all the domains on
    9.83 + * it. When called, the domain information has already been collected. */
    9.84 +typedef int (*xenstat_collect_func)(xenstat_handle * handle,
    9.85 +				    xenstat_node * node);
    9.86 +/* Called to free the information collected by the collect function.  The free
    9.87 + * function will only be called on a xenstat_node if that node includes
    9.88 + * information collected by the corresponding collector. */
    9.89 +typedef void (*xenstat_free_func)(xenstat_node * node);
    9.90 +/* Called to free any information stored in the handle.  Note the lack of a
    9.91 + * matching init function; the collect functions should initialize on first
    9.92 + * use.  Also, the uninit function must handle the case that the collector has
    9.93 + * never been initialized. */
    9.94 +typedef void (*xenstat_uninit_func)(xenstat_handle * handle);
    9.95 +typedef struct xenstat_collector {
    9.96 +	unsigned int flag;
    9.97 +	xenstat_collect_func collect;
    9.98 +	xenstat_free_func free;
    9.99 +	xenstat_uninit_func uninit;
   9.100 +} xenstat_collector;
   9.101 +
   9.102 +static int  xenstat_collect_vcpus(xenstat_handle * handle,
   9.103 +				  xenstat_node * node);
   9.104 +static int  xenstat_collect_networks(xenstat_handle * handle,
   9.105 +				    xenstat_node * node);
   9.106 +static void xenstat_free_vcpus(xenstat_node * node);
   9.107 +static void xenstat_free_networks(xenstat_node * node);
   9.108 +static void xenstat_uninit_vcpus(xenstat_handle * handle);
   9.109 +static void xenstat_uninit_networks(xenstat_handle * handle);
   9.110 +
   9.111 +static xenstat_collector collectors[] = {
   9.112 +	{ XENSTAT_VCPU, xenstat_collect_vcpus,
   9.113 +	  xenstat_free_vcpus, xenstat_uninit_vcpus },
   9.114 +	{ XENSTAT_NETWORK, xenstat_collect_networks,
   9.115 +	  xenstat_free_networks, xenstat_uninit_networks }
   9.116 +};
   9.117 +
   9.118 +#define NUM_COLLECTORS (sizeof(collectors)/sizeof(xenstat_collector))
   9.119 +
   9.120 +/*
   9.121 + * libxenstat API
   9.122 + */
   9.123 +xenstat_handle *xenstat_init()
   9.124 +{
   9.125 +	xenstat_handle *handle;
   9.126 +
   9.127 +	handle = (xenstat_handle *) calloc(1, sizeof(xenstat_handle));
   9.128 +	if (handle == NULL)
   9.129 +		return NULL;
   9.130 +
   9.131 +#if defined(PAGESIZE)
   9.132 +	handle->page_size = PAGESIZE;
   9.133 +#elif defined(PAGE_SIZE)
   9.134 +	handle->page_size = PAGE_SIZE;
   9.135 +#else
   9.136 +	handle->page_size = sysconf(_SC_PAGE_SIZE);
   9.137 +	if (handle->page_size < 0) {
   9.138 +		perror("Failed to retrieve page size.");
   9.139 +		free(handle);
   9.140 +		return NULL;
   9.141 +	}
   9.142 +#endif
   9.143 +
   9.144 +	handle->xihandle = xi_init();
   9.145 +	if (handle->xihandle == NULL) {
   9.146 +		perror("xi_init");
   9.147 +		free(handle);
   9.148 +		return NULL;
   9.149 +	}
   9.150 +
   9.151 +	return handle;
   9.152 +}
   9.153 +
   9.154 +void xenstat_uninit(xenstat_handle * handle)
   9.155 +{
   9.156 +	unsigned int i;
   9.157 +	if (handle) {
   9.158 +		for (i = 0; i < NUM_COLLECTORS; i++)
   9.159 +			collectors[i].uninit(handle);
   9.160 +		xi_uninit(handle->xihandle);
   9.161 +		free(handle);
   9.162 +	}
   9.163 +}
   9.164 +
   9.165 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags)
   9.166 +{
   9.167 +#define DOMAIN_CHUNK_SIZE 256
   9.168 +	xenstat_node *node;
   9.169 +	dom0_physinfo_t physinfo;
   9.170 +	dom0_getdomaininfo_t domaininfo[DOMAIN_CHUNK_SIZE];
   9.171 +	unsigned int num_domains, new_domains;
   9.172 +	unsigned int i;
   9.173 +
   9.174 +	/* Create the node */
   9.175 +	node = (xenstat_node *) calloc(1, sizeof(xenstat_node));
   9.176 +	if (node == NULL)
   9.177 +		return NULL;
   9.178 +
   9.179 +	/* Get information about the physical system */
   9.180 +	if (xi_get_physinfo(handle->xihandle, &physinfo) < 0) {
   9.181 +		free(node);
   9.182 +		return NULL;
   9.183 +	}
   9.184 +
   9.185 +	node->cpu_hz = ((unsigned long long)physinfo.cpu_khz) * 1000ULL;
   9.186 +	node->num_cpus =
   9.187 +	    (physinfo.threads_per_core * physinfo.cores_per_socket *
   9.188 +	     physinfo.sockets_per_node * physinfo.nr_nodes);
   9.189 +	node->tot_mem = ((unsigned long long)physinfo.total_pages)
   9.190 +	    * handle->page_size;
   9.191 +	node->free_mem = ((unsigned long long)physinfo.free_pages)
   9.192 +	    * handle->page_size;
   9.193 +
   9.194 +	/* malloc(0) is not portable, so allocate a single domain.  This will
   9.195 +	 * be resized below. */
   9.196 +	node->domains = malloc(sizeof(xenstat_domain));
   9.197 +	if (node->domains == NULL) {
   9.198 +		free(node);
   9.199 +		return NULL;
   9.200 +	}
   9.201 +
   9.202 +	num_domains = 0;
   9.203 +	do {
   9.204 +		xenstat_domain *domain;
   9.205 +
   9.206 +		new_domains = xi_get_domaininfolist(handle->xihandle,
   9.207 +		                                    domaininfo, num_domains,
   9.208 +		                                    DOMAIN_CHUNK_SIZE);
   9.209 +
   9.210 +		node->domains = realloc(node->domains,
   9.211 +					(num_domains + new_domains)
   9.212 +					* sizeof(xenstat_domain));
   9.213 +		if (node->domains == NULL) {
   9.214 +			free(node);
   9.215 +			return NULL;
   9.216 +		}
   9.217 +
   9.218 +		domain = node->domains + num_domains;
   9.219 +
   9.220 +		for (i = 0; i < new_domains; i++) {
   9.221 +			/* Fill in domain using domaininfo[i] */
   9.222 +			domain->id = domaininfo[i].domain;
   9.223 +			domain->state = domaininfo[i].flags;
   9.224 +			domain->cpu_ns = domaininfo[i].cpu_time;
   9.225 +			domain->num_vcpus = domaininfo[i].n_vcpu;
   9.226 +			domain->vcpus = NULL;
   9.227 +			domain->cur_mem =
   9.228 +			    ((unsigned long long)domaininfo[i].tot_pages)
   9.229 +			    * handle->page_size;
   9.230 +			domain->max_mem =
   9.231 +			    domaininfo[i].max_pages == UINT_MAX
   9.232 +			    ? (unsigned long long)-1
   9.233 +			    : (unsigned long long)(domaininfo[i].max_pages
   9.234 +						   * handle->page_size);
   9.235 +			domain->ssid = domaininfo[i].ssidref;
   9.236 +			domain->num_networks = 0;
   9.237 +			domain->networks = NULL;
   9.238 +
   9.239 +			domain++;
   9.240 +		}
   9.241 +		num_domains += new_domains;
   9.242 +	} while (new_domains == DOMAIN_CHUNK_SIZE);
   9.243 +	node->num_domains = num_domains;
   9.244 +
   9.245 +	/* Run all the extra data collectors requested */
   9.246 +	node->flags = 0;
   9.247 +	for (i = 0; i < NUM_COLLECTORS; i++) {
   9.248 +		if ((flags & collectors[i].flag) == collectors[i].flag) {
   9.249 +			node->flags |= collectors[i].flag;
   9.250 +			if(collectors[i].collect(handle, node) == 0) {
   9.251 +				xenstat_free_node(node);
   9.252 +				return NULL;
   9.253 +                        }
   9.254 +                }
   9.255 +	}
   9.256 +
   9.257 +	return node;
   9.258 +}
   9.259 +
   9.260 +void xenstat_free_node(xenstat_node * node)
   9.261 +{
   9.262 +	int i;
   9.263 +
   9.264 +	if (node) {
   9.265 +		if (node->domains) {
   9.266 +			for (i = 0; i < NUM_COLLECTORS; i++)
   9.267 +				if((node->flags & collectors[i].flag)
   9.268 +				   == collectors[i].flag)
   9.269 +					collectors[i].free(node);
   9.270 +			free(node->domains);
   9.271 +		}
   9.272 +		free(node);
   9.273 +	}
   9.274 +}
   9.275 +
   9.276 +xenstat_domain *xenstat_node_domain(xenstat_node * node, unsigned int domid)
   9.277 +{
   9.278 +	unsigned int i;
   9.279 +
   9.280 +	/* FIXME: binary search */
   9.281 +	/* Find the appropriate domain entry in the node struct. */
   9.282 +	for (i = 0; i < node->num_domains; i++) {
   9.283 +		if (node->domains[i].id == domid)
   9.284 +			return &(node->domains[i]);
   9.285 +	}
   9.286 +	return NULL;
   9.287 +}
   9.288 +
   9.289 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
   9.290 +					     unsigned int index)
   9.291 +{
   9.292 +	if (0 <= index && index < node->num_domains)
   9.293 +		return &(node->domains[index]);
   9.294 +	return NULL;
   9.295 +}
   9.296 +
   9.297 +unsigned long long xenstat_node_tot_mem(xenstat_node * node)
   9.298 +{
   9.299 +	return node->tot_mem;
   9.300 +}
   9.301 +
   9.302 +unsigned long long xenstat_node_free_mem(xenstat_node * node)
   9.303 +{
   9.304 +	return node->free_mem;
   9.305 +}
   9.306 +
   9.307 +unsigned int xenstat_node_num_domains(xenstat_node * node)
   9.308 +{
   9.309 +	return node->num_domains;
   9.310 +}
   9.311 +
   9.312 +unsigned int xenstat_node_num_cpus(xenstat_node * node)
   9.313 +{
   9.314 +	return node->num_cpus;
   9.315 +}
   9.316 +
   9.317 +/* Get information about the CPU speed */
   9.318 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node)
   9.319 +{
   9.320 +	return node->cpu_hz;
   9.321 +}
   9.322 +
   9.323 +/* Get the domain ID for this domain */
   9.324 +unsigned xenstat_domain_id(xenstat_domain * domain)
   9.325 +{
   9.326 +	return domain->id;
   9.327 +}
   9.328 +
   9.329 +/* Get information about how much CPU time has been used */
   9.330 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain)
   9.331 +{
   9.332 +	return domain->cpu_ns;
   9.333 +}
   9.334 +
   9.335 +/* Find the number of VCPUs allocated to a domain */
   9.336 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain)
   9.337 +{
   9.338 +	return domain->num_vcpus;
   9.339 +}
   9.340 +
   9.341 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain, unsigned int vcpu)
   9.342 +{
   9.343 +	if (0 <= vcpu && vcpu < domain->num_vcpus)
   9.344 +		return &(domain->vcpus[vcpu]);
   9.345 +	return NULL;
   9.346 +}
   9.347 +
   9.348 +/* Find the current memory reservation for this domain */
   9.349 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain)
   9.350 +{
   9.351 +	return domain->cur_mem;
   9.352 +}
   9.353 +
   9.354 +/* Find the maximum memory reservation for this domain */
   9.355 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain)
   9.356 +{
   9.357 +	return domain->max_mem;
   9.358 +}
   9.359 +
   9.360 +/* Find the domain's SSID */
   9.361 +unsigned int xenstat_domain_ssid(xenstat_domain * domain)
   9.362 +{
   9.363 +	return domain->ssid;
   9.364 +}
   9.365 +
   9.366 +/* Get domain states */
   9.367 +unsigned int xenstat_domain_dying(xenstat_domain * domain)
   9.368 +{
   9.369 +	return (domain->state & DOMFLAGS_DYING) == DOMFLAGS_DYING;
   9.370 +}
   9.371 +
   9.372 +unsigned int xenstat_domain_crashed(xenstat_domain * domain)
   9.373 +{
   9.374 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
   9.375 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
   9.376 +		 & DOMFLAGS_SHUTDOWNMASK) == SHUTDOWN_crash);
   9.377 +}
   9.378 +
   9.379 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain)
   9.380 +{
   9.381 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
   9.382 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
   9.383 +		 & DOMFLAGS_SHUTDOWNMASK) != SHUTDOWN_crash);
   9.384 +}
   9.385 +
   9.386 +unsigned int xenstat_domain_paused(xenstat_domain * domain)
   9.387 +{
   9.388 +	return (domain->state & DOMFLAGS_PAUSED) == DOMFLAGS_PAUSED;
   9.389 +}
   9.390 +
   9.391 +unsigned int xenstat_domain_blocked(xenstat_domain * domain)
   9.392 +{
   9.393 +	return (domain->state & DOMFLAGS_BLOCKED) == DOMFLAGS_BLOCKED;
   9.394 +}
   9.395 +
   9.396 +unsigned int xenstat_domain_running(xenstat_domain * domain)
   9.397 +{
   9.398 +	return (domain->state & DOMFLAGS_RUNNING) == DOMFLAGS_RUNNING;
   9.399 +}
   9.400 +
   9.401 +/* Get the number of networks for a given domain */
   9.402 +unsigned int xenstat_domain_num_networks(xenstat_domain * domain)
   9.403 +{
   9.404 +	return domain->num_networks;
   9.405 +}
   9.406 +
   9.407 +/* Get the network handle to obtain network stats */
   9.408 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
   9.409 +					unsigned int network)
   9.410 +{
   9.411 +	if (domain->networks && 0 <= network && network < domain->num_networks)
   9.412 +		return &(domain->networks[network]);
   9.413 +	return NULL;
   9.414 +}
   9.415 +
   9.416 +/*
   9.417 + * VCPU functions
   9.418 + */
   9.419 +/* Collect information about VCPUs */
   9.420 +static int xenstat_collect_vcpus(xenstat_handle * handle, xenstat_node * node)
   9.421 +{
   9.422 +	unsigned int i, vcpu;
   9.423 +	/* Fill in VCPU information */
   9.424 +	for (i = 0; i < node->num_domains; i++) {
   9.425 +		node->domains[i].vcpus = malloc(node->domains[i].num_vcpus
   9.426 +						* sizeof(xenstat_vcpu));
   9.427 +		if (node->domains[i].vcpus == NULL)
   9.428 +			return 0;
   9.429 +
   9.430 +		for (vcpu = 0; vcpu < node->domains[i].num_vcpus; vcpu++) {
   9.431 +			/* FIXME: need to be using a more efficient mechanism*/
   9.432 +			long long vcpu_time;
   9.433 +			vcpu_time =
   9.434 +			    xi_get_vcpu_usage(handle->xihandle,
   9.435 +					      node->domains[i].id,
   9.436 +					      vcpu);
   9.437 +			if (vcpu_time < 0)
   9.438 +				return 0;
   9.439 +			node->domains[i].vcpus[vcpu].ns = vcpu_time;
   9.440 +		}
   9.441 +	}
   9.442 +	return 1;
   9.443 +}
   9.444 +
   9.445 +/* Free VCPU information */
   9.446 +static void xenstat_free_vcpus(xenstat_node * node)
   9.447 +{
   9.448 +	unsigned int i;
   9.449 +	for (i = 0; i < node->num_domains; i++)
   9.450 +		free(node->domains[i].vcpus);
   9.451 +}
   9.452 +
   9.453 +/* Free VCPU information in handle - nothing to do */
   9.454 +static void xenstat_uninit_vcpus(xenstat_handle * handle)
   9.455 +{
   9.456 +}
   9.457 +
   9.458 +/* Get VCPU usage */
   9.459 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu)
   9.460 +{
   9.461 +	return vcpu->ns;
   9.462 +}
   9.463 +
   9.464 +/*
   9.465 + * Network functions
   9.466 + */
   9.467 +
   9.468 +/* Expected format of /proc/net/dev */
   9.469 +static const char PROCNETDEV_HEADER[] =
   9.470 +    "Inter-|   Receive                                                |"
   9.471 +    "  Transmit\n"
   9.472 +    " face |bytes    packets errs drop fifo frame compressed multicast|"
   9.473 +    "bytes    packets errs drop fifo colls carrier compressed\n";
   9.474 +
   9.475 +/* Collect information about networks */
   9.476 +static int xenstat_collect_networks(xenstat_handle * handle,
   9.477 +				    xenstat_node * node)
   9.478 +{
   9.479 +	/* Open and validate /proc/net/dev if we haven't already */
   9.480 +	if (handle->procnetdev == NULL) {
   9.481 +		char header[sizeof(PROCNETDEV_HEADER)];
   9.482 +		handle->procnetdev = fopen("/proc/net/dev", "r");
   9.483 +		if (handle->procnetdev == NULL) {
   9.484 +			perror("Error opening /proc/net/dev");
   9.485 +			return 1;
   9.486 +		}
   9.487 +
   9.488 +		/* Validate the format of /proc/net/dev */
   9.489 +		if (fread(header, sizeof(PROCNETDEV_HEADER) - 1, 1,
   9.490 +			  handle->procnetdev) != 1) {
   9.491 +			perror("Error reading /proc/net/dev header");
   9.492 +			return 1;
   9.493 +		}
   9.494 +		header[sizeof(PROCNETDEV_HEADER) - 1] = '\0';
   9.495 +		if (strcmp(header, PROCNETDEV_HEADER) != 0) {
   9.496 +			fprintf(stderr,
   9.497 +				"Unexpected /proc/net/dev format\n");
   9.498 +			return 1;
   9.499 +		}
   9.500 +	}
   9.501 +
   9.502 +	/* Fill in networks */
   9.503 +	/* FIXME: optimize this */
   9.504 +	fseek(handle->procnetdev, sizeof(PROCNETDEV_HEADER) - 1, SEEK_SET);
   9.505 +	while (1) {
   9.506 +		xenstat_domain *domain;
   9.507 +		xenstat_network net;
   9.508 +		unsigned int domid;
   9.509 +		int ret = fscanf(handle->procnetdev,
   9.510 +				 "vif%u.%u:%llu%llu%llu%llu%*u%*u%*u%*u"
   9.511 +				 "%llu%llu%llu%llu%*u%*u%*u%*u",
   9.512 +				 &domid, &net.id,
   9.513 +				 &net.tbytes, &net.tpackets, &net.terrs,
   9.514 +				 &net.tdrop,
   9.515 +				 &net.rbytes, &net.rpackets, &net.rerrs,
   9.516 +				 &net.rdrop);
   9.517 +		if (ret == EOF)
   9.518 +			break;
   9.519 +		if (ret != 10) {
   9.520 +			unsigned int c;
   9.521 +			do {
   9.522 +				c = fgetc(handle->procnetdev);
   9.523 +			} while (c != '\n' && c != EOF);
   9.524 +			if (c == EOF)
   9.525 +				break;
   9.526 +			continue;
   9.527 +		}
   9.528 +
   9.529 +		/* FIXME: this does a search for the domid */
   9.530 +		domain = xenstat_node_domain(node, domid);
   9.531 +		if (domain == NULL) {
   9.532 +			fprintf(stderr,
   9.533 +				"Found interface vif%u.%u but domain %u"
   9.534 +				" does not exist.\n", domid, net.id,
   9.535 +				domid);
   9.536 +			continue;
   9.537 +		}
   9.538 +		if (domain->networks == NULL) {
   9.539 +			domain->num_networks = 1;
   9.540 +			domain->networks = malloc(sizeof(xenstat_network));
   9.541 +		} else {
   9.542 +			domain->num_networks++;
   9.543 +			domain->networks =
   9.544 +			    realloc(domain->networks,
   9.545 +				    domain->num_networks *
   9.546 +				    sizeof(xenstat_network));
   9.547 +		}
   9.548 +		if (domain->networks == NULL)
   9.549 +			return 1;
   9.550 +		domain->networks[domain->num_networks - 1] = net;
   9.551 +	}
   9.552 +
   9.553 +	return 1;
   9.554 +}
   9.555 +
   9.556 +/* Free network information */
   9.557 +static void xenstat_free_networks(xenstat_node * node)
   9.558 +{
   9.559 +	unsigned int i;
   9.560 +	for (i = 0; i < node->num_domains; i++)
   9.561 +		free(node->domains[i].networks);
   9.562 +}
   9.563 +
   9.564 +/* Free network information in handle */
   9.565 +static void xenstat_uninit_networks(xenstat_handle * handle)
   9.566 +{
   9.567 +	if(handle->procnetdev)
   9.568 +		fclose(handle->procnetdev);
   9.569 +}
   9.570 +
   9.571 +/* Get the network ID */
   9.572 +unsigned int xenstat_network_id(xenstat_network * network)
   9.573 +{
   9.574 +	return network->id;
   9.575 +}
   9.576 +
   9.577 +/* Get the number of receive bytes */
   9.578 +unsigned long long xenstat_network_rbytes(xenstat_network * network)
   9.579 +{
   9.580 +	return network->rbytes;
   9.581 +}
   9.582 +
   9.583 +/* Get the number of receive packets */
   9.584 +unsigned long long xenstat_network_rpackets(xenstat_network * network)
   9.585 +{
   9.586 +	return network->rpackets;
   9.587 +}
   9.588 +
   9.589 +/* Get the number of receive errors */
   9.590 +unsigned long long xenstat_network_rerrs(xenstat_network * network)
   9.591 +{
   9.592 +	return network->rerrs;
   9.593 +}
   9.594 +
   9.595 +/* Get the number of receive drops */
   9.596 +unsigned long long xenstat_network_rdrop(xenstat_network * network)
   9.597 +{
   9.598 +	return network->rdrop;
   9.599 +}
   9.600 +
   9.601 +/* Get the number of transmit bytes */
   9.602 +unsigned long long xenstat_network_tbytes(xenstat_network * network)
   9.603 +{
   9.604 +	return network->tbytes;
   9.605 +}
   9.606 +
   9.607 +/* Get the number of transmit packets */
   9.608 +unsigned long long xenstat_network_tpackets(xenstat_network * network)
   9.609 +{
   9.610 +	return network->tpackets;
   9.611 +}
   9.612 +
   9.613 +/* Get the number of transmit errors */
   9.614 +unsigned long long xenstat_network_terrs(xenstat_network * network)
   9.615 +{
   9.616 +	return network->terrs;
   9.617 +}
   9.618 +
   9.619 +/* Get the number of transmit dropped packets */
   9.620 +unsigned long long xenstat_network_tdrop(xenstat_network * network)
   9.621 +{
   9.622 +	return network->tdrop;
   9.623 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/tools/xenstat/libxenstat/src/xenstat.h	Fri Aug 12 18:36:54 2005 +0000
    10.3 @@ -0,0 +1,148 @@
    10.4 +/* libxenstat: statistics-collection library for Xen
    10.5 + * Copyright (C) International Business Machines Corp., 2005
    10.6 + * Authors: Josh Triplett <josht@us.ibm.com>
    10.7 + *          Judy Fischbach <jfisch@us.ibm.com>
    10.8 + *          David Hendricks <dhendrix@us.ibm.com>
    10.9 + *
   10.10 + * This library is free software; you can redistribute it and/or
   10.11 + * modify it under the terms of the GNU Lesser General Public
   10.12 + * License as published by the Free Software Foundation; either
   10.13 + * version 2.1 of the License, or (at your option) any later version.
   10.14 + *
   10.15 + * This library is distributed in the hope that it will be useful,
   10.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   10.18 + * Lesser General Public License for more details.
   10.19 + */
   10.20 +
   10.21 +/* libxenstat API */
   10.22 +
   10.23 +/* Opaque handles */
   10.24 +typedef struct xenstat_handle xenstat_handle;
   10.25 +typedef struct xenstat_domain xenstat_domain;
   10.26 +typedef struct xenstat_node xenstat_node;
   10.27 +typedef struct xenstat_vcpu xenstat_vcpu;
   10.28 +typedef struct xenstat_network xenstat_network;
   10.29 +
   10.30 +/* Initialize the xenstat library.  Returns a handle to be used with
   10.31 + * subsequent calls to the xenstat library, or NULL if an error occurs. */
   10.32 +xenstat_handle *xenstat_init();
   10.33 +
   10.34 +/* Release the handle to libxc, free resources, etc. */
   10.35 +void xenstat_uninit(xenstat_handle * handle);
   10.36 +
   10.37 +/* Get all available information about a node */
   10.38 +#define XENSTAT_VCPU 0x1
   10.39 +#define XENSTAT_NETWORK 0x2
   10.40 +#define XENSTAT_ALL (XENSTAT_VCPU|XENSTAT_NETWORK)
   10.41 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags);
   10.42 +
   10.43 +/* Free the information */
   10.44 +void xenstat_free_node(xenstat_node * node);
   10.45 +
   10.46 +/*
   10.47 + * Node functions - extract information from a xenstat_node
   10.48 + */
   10.49 +
   10.50 +/* Get information about the domain with the given domain ID */
   10.51 +xenstat_domain *xenstat_node_domain(xenstat_node * node,
   10.52 +				    unsigned int domid);
   10.53 +
   10.54 +/* Get the domain with the given index; used to loop over all domains. */
   10.55 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
   10.56 +					     unsigned index);
   10.57 +
   10.58 +/* Get amount of total memory on a node */
   10.59 +unsigned long long xenstat_node_tot_mem(xenstat_node * node);
   10.60 +
   10.61 +/* Get amount of free memory on a node */
   10.62 +unsigned long long xenstat_node_free_mem(xenstat_node * node);
   10.63 +
   10.64 +/* Find the number of domains existing on a node */
   10.65 +unsigned int xenstat_node_num_domains(xenstat_node * node);
   10.66 +
   10.67 +/* Find the number of CPUs existing on a node */
   10.68 +unsigned int xenstat_node_num_cpus(xenstat_node * node);
   10.69 +
   10.70 +/* Get information about the CPU speed */
   10.71 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node);
   10.72 +
   10.73 +/*
   10.74 + * Domain functions - extract information from a xenstat_domain
   10.75 + */
   10.76 +
   10.77 +/* Get the domain ID for this domain */
   10.78 +unsigned xenstat_domain_id(xenstat_domain * domain);
   10.79 +
   10.80 +/* Get information about how much CPU time has been used */
   10.81 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain);
   10.82 +
   10.83 +/* Find the number of VCPUs allocated to a domain */
   10.84 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain);
   10.85 +
   10.86 +/* Get the VCPU handle to obtain VCPU stats */
   10.87 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain,
   10.88 +				  unsigned int vcpu);
   10.89 +
   10.90 +/* Find the current memory reservation for this domain */
   10.91 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain);
   10.92 +
   10.93 +/* Find the maximum memory reservation for this domain */
   10.94 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain);
   10.95 +
   10.96 +/* Find the domain's SSID */
   10.97 +unsigned int xenstat_domain_ssid(xenstat_domain * domain);
   10.98 +
   10.99 +/* Get domain states */
  10.100 +unsigned int xenstat_domain_dying(xenstat_domain * domain);
  10.101 +unsigned int xenstat_domain_crashed(xenstat_domain * domain);
  10.102 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain);
  10.103 +unsigned int xenstat_domain_paused(xenstat_domain * domain);
  10.104 +unsigned int xenstat_domain_blocked(xenstat_domain * domain);
  10.105 +unsigned int xenstat_domain_running(xenstat_domain * domain);
  10.106 +
  10.107 +/* Get the number of networks for a given domain */
  10.108 +unsigned int xenstat_domain_num_networks(xenstat_domain *);
  10.109 +
  10.110 +/* Get the network handle to obtain network stats */
  10.111 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
  10.112 +					unsigned int network);
  10.113 +
  10.114 +/*
  10.115 + * VCPU functions - extract information from a xenstat_vcpu
  10.116 + */
  10.117 +
  10.118 +/* Get VCPU usage */
  10.119 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu);
  10.120 +
  10.121 +
  10.122 +/*
  10.123 + * Network functions - extract information from a xenstat_network
  10.124 + */
  10.125 +
  10.126 +/* Get the ID for this network */
  10.127 +unsigned int xenstat_network_id(xenstat_network * network);
  10.128 +
  10.129 +/* Get the number of receive bytes for this network */
  10.130 +unsigned long long xenstat_network_rbytes(xenstat_network * network);
  10.131 +
  10.132 +/* Get the number of receive packets for this network */
  10.133 +unsigned long long xenstat_network_rpackets(xenstat_network * network);
  10.134 +
  10.135 +/* Get the number of receive errors for this network */
  10.136 +unsigned long long xenstat_network_rerrs(xenstat_network * network);
  10.137 +
  10.138 +/* Get the number of receive drops for this network */
  10.139 +unsigned long long xenstat_network_rdrop(xenstat_network * network);
  10.140 +
  10.141 +/* Get the number of transmit bytes for this network */
  10.142 +unsigned long long xenstat_network_tbytes(xenstat_network * network);
  10.143 +
  10.144 +/* Get the number of transmit packets for this network */
  10.145 +unsigned long long xenstat_network_tpackets(xenstat_network * network);
  10.146 +
  10.147 +/* Get the number of transmit errors for this network */
  10.148 +unsigned long long xenstat_network_terrs(xenstat_network * network);
  10.149 +
  10.150 +/* Get the number of transmit drops for this network */
  10.151 +unsigned long long xenstat_network_tdrop(xenstat_network * network);
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/tools/xenstat/vm-top/Makefile	Fri Aug 12 18:36:54 2005 +0000
    11.3 @@ -0,0 +1,47 @@
    11.4 +# Copyright (C) International Business Machines Corp., 2005
    11.5 +# Author: Josh Triplett <josht@us.ibm.com>
    11.6 +# 
    11.7 +# This program is free software; you can redistribute it and/or modify
    11.8 +# it under the terms of the GNU General Public License as published by
    11.9 +# the Free Software Foundation; under version 2 of the License.
   11.10 +#
   11.11 +# This program is distributed in the hope that it will be useful,
   11.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.14 +# GNU General Public License for more details.
   11.15 +
   11.16 +ifneq (,$(shell [ -e /usr/include/curses.h ] || echo no))
   11.17 +all vm-top:
   11.18 +	@echo "***********************************************************"
   11.19 +	@echo "WARNING: Install ncurses to build vm-top."
   11.20 +	@echo "***********************************************************"
   11.21 +else
   11.22 +
   11.23 +XEN_ROOT=../../..
   11.24 +include $(XEN_ROOT)/tools/Rules.mk
   11.25 +
   11.26 +INSTALL         = install
   11.27 +INSTALL_PROG    = $(INSTALL) -m0755 -D
   11.28 +INSTALL_DATA    = $(INSTALL) -m0644 -D
   11.29 +
   11.30 +prefix=/usr
   11.31 +mandir=$(prefix)/share/man
   11.32 +man1dir=$(mandir)/man1
   11.33 +sbindir=$(prefix)/sbin
   11.34 +
   11.35 +CFLAGS += -DGCC_PRINTF -Wall -Werror -I$(XEN_LIBXENSTAT)
   11.36 +LDFLAGS += -L$(XEN_LIBXENSTAT) -lxenstat -lcurses
   11.37 +
   11.38 +all: vm-top
   11.39 +
   11.40 +vm-top: vm-top.o
   11.41 +
   11.42 +endif
   11.43 +
   11.44 +install: vm-top vm-top.1
   11.45 +	[ -f vm-top ] && $(INSTALL_PROG) vm-top $(DESTDIR)$(sbindir)/vm-top
   11.46 +	[ -f vm-top.1 ] && \
   11.47 +	    $(INSTALL_DATA) vm-top.1 $(DESTDIR)$(man1dir)/vm-top.1
   11.48 +
   11.49 +clean:
   11.50 +	rm -f vm-top vm-top.o
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/tools/xenstat/vm-top/TODO	Fri Aug 12 18:36:54 2005 +0000
    12.3 @@ -0,0 +1,37 @@
    12.4 +Inefficient about how it handles newlines in vm-top 
    12.5 +for proc/net/dev. Fix.
    12.6 +
    12.7 +Display error messages on the help line after bad input at a prompt.
    12.8 +Fractional delay times
    12.9 +Use prompting to search for domains
   12.10 +Better line editing?
   12.11 +
   12.12 +* Make CPU in % more accurate
   12.13 +* Domain total network TX % and RX %
   12.14 +
   12.15 +Like Top, f feature, field select of domain columns, toggle the display of
   12.16 +field by typing the letter associated with field, if displayed it shows in
   12.17 +bold and the letter is Capitalized along with a leading asterisk for the
   12.18 +field, if not selected for display letter is lowercase, no leading asterisk
   12.19 +and field is not bolded.
   12.20 +
   12.21 +Like Top, ordering of domain columns, o feature Capital letter shifts left,
   12.22 +lowercase letter shifts right?
   12.23 +
   12.24 +Color
   12.25 +Full management: pause, destroy, create domains
   12.26 +
   12.27 +Add support for Virtual Block Devices (vbd)
   12.28 +
   12.29 +To think about:
   12.30 +Support for one than one node display (distributed monitoring 
   12.31 +from any node of all other nodes in a cluster)
   12.32 +Bottom line option (Switch node, Search node [tab completion?])
   12.33 +
   12.34 +Capture/Logging of resource information generated during a time interval.
   12.35 +-b batch mode dump snapshots to standard output (used with -n)
   12.36 +-n number of iterations to dump to standard output (unlimited if not specified)
   12.37 +-d monitor DomIDs as -dD1,-dD2 or -dD1,D2...
   12.38 +   Monitor only domains with specified domain IDs
   12.39 +-m monitor nodeIDs as -mN1,-mN2 or -mN1,N2...
   12.40 +   Monitor only domains with specified node IDs
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/tools/xenstat/vm-top/vm-top.1	Fri Aug 12 18:36:54 2005 +0000
    13.3 @@ -0,0 +1,88 @@
    13.4 +.\" Copyright (C) International Business Machines  Corp., 2005
    13.5 +.\" Author: Josh Triplett <josht@us.ibm.com>
    13.6 +.\"
    13.7 +.\" This program is free software; you can redistribute it and/or modify
    13.8 +.\" it under the terms of the GNU General Public License as published by
    13.9 +.\" the Free Software Foundation; under version 2 of the License.
   13.10 +.\"
   13.11 +.\" This program is distributed in the hope that it will be useful,
   13.12 +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
   13.13 +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13.14 +.\" GNU General Public License for more details.
   13.15 +.\"
   13.16 +.\" You should have received a copy of the GNU General Public License
   13.17 +.\" along with this program; if not, write to the Free Software
   13.18 +.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   13.19 +.TH vm-top 1 "August 2005"
   13.20 +.SH NAME
   13.21 +\fBvm-top\fR \- displays real-time information about a Xen system and domains
   13.22 +
   13.23 +.SH SYNOPSIS
   13.24 +.B vm-top
   13.25 +[\fB\-h\fR]
   13.26 +[\fB\-V\fR]
   13.27 +[\fB\-d\fRSECONDS]
   13.28 +[\fB\-n\fR]
   13.29 +[\fB\-r\fR]
   13.30 +[\fB\-v\fR]
   13.31 +
   13.32 +.SH DESCRIPTION
   13.33 +\fBvm-top\fR displays information about the Xen system and domains, in a
   13.34 +continually-updating manner.  Command-line options and interactive commands
   13.35 +can change the detail and format of the information displayed by \fBvm-top\fR.
   13.36 +
   13.37 +.SH OPTIONS
   13.38 +.TP
   13.39 +\fB\-h\fR, \fB\-\-help\fR
   13.40 +display help and exit
   13.41 +.TP
   13.42 +\fB\-V\fR, \fB\-\-version\fR
   13.43 +output version information and exit
   13.44 +.TP
   13.45 +\fB\-d\fR, \fB\-\-delay\fR=\fISECONDS\fR
   13.46 +seconds between updates (default 1)
   13.47 +.TP
   13.48 +\fB\-n\fR, \fB\-\-networks\fR
   13.49 +output network information
   13.50 +.TP
   13.51 +\fB\-r\fR, \fB\-\-repeat\-header\fR
   13.52 +repeat table header before each domain
   13.53 +.TP
   13.54 +\fB\-v\fR, \fB\-\-vcpus\fR
   13.55 +output VCPU data
   13.56 +
   13.57 +.SH "INTERACTIVE COMMANDS"
   13.58 +All interactive commands are case-insensitive.
   13.59 +.TP
   13.60 +.B D
   13.61 +set delay between updates
   13.62 +.TP
   13.63 +.B N
   13.64 +toggle display of network information
   13.65 +.TP
   13.66 +.B Q, Esc
   13.67 +quit
   13.68 +.TP
   13.69 +.B R
   13.70 +toggle table header before each domain
   13.71 +.TP
   13.72 +.B S
   13.73 +cycle sort order
   13.74 +.TP
   13.75 +.B V
   13.76 +toggle display of VCPU information
   13.77 +.TP
   13.78 +.B Arrows
   13.79 +scroll domain display
   13.80 +
   13.81 +.SH AUTHORS
   13.82 +Written by Judy Fischbach, David Hendricks, and Josh Triplett
   13.83 +
   13.84 +.SH "REPORTING BUGS"
   13.85 +Report bugs to <dsteklof@us.ibm.com>.
   13.86 +
   13.87 +.SH COPYRIGHT
   13.88 +Copyright \(co 2005  International Business Machines  Corp
   13.89 +.br
   13.90 +This is free software; see the source for copying conditions.  There is NO
   13.91 +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/tools/xenstat/vm-top/vm-top.c	Fri Aug 12 18:36:54 2005 +0000
    14.3 @@ -0,0 +1,880 @@
    14.4 +/*
    14.5 + *  Copyright (C) International Business Machines  Corp., 2005
    14.6 + *  Author(s): Judy Fischbach <jfisch@us.ibm.com>
    14.7 + *             David Hendricks <dhendrix@us.ibm.com>
    14.8 + *             Josh Triplett <josht@us.ibm.com>
    14.9 + *    based on code from Anthony Liguori <aliguori@us.ibm.com>
   14.10 + *
   14.11 + *  This program is free software; you can redistribute it and/or modify
   14.12 + *  it under the terms of the GNU General Public License as published by
   14.13 + *  the Free Software Foundation; under version 2 of the License.
   14.14 + *
   14.15 + *  This program is distributed in the hope that it will be useful,
   14.16 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   14.17 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14.18 + *  GNU General Public License for more details.
   14.19 + *
   14.20 + *  You should have received a copy of the GNU General Public License
   14.21 + *  along with this program; if not, write to the Free Software
   14.22 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   14.23 + */
   14.24 +#include <curses.h>
   14.25 +#include <ctype.h>
   14.26 +#include <errno.h>
   14.27 +#include <stdio.h>
   14.28 +#include <stdlib.h>
   14.29 +#include <string.h>
   14.30 +#include <sys/time.h>
   14.31 +#include <time.h>
   14.32 +#include <unistd.h>
   14.33 +
   14.34 +#include <xenstat.h>
   14.35 +
   14.36 +#define VM_TOP_VERSION "1.0"
   14.37 +
   14.38 +#define VM_TOP_DISCLAIMER \
   14.39 +"Copyright (C) 2005  International Business Machines  Corp\n"\
   14.40 +"This is free software; see the source for copying conditions.There is NO\n"\
   14.41 +"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
   14.42 +#define VM_TOP_BUGSTO "Report bugs to <dsteklof@us.ibm.com>.\n"
   14.43 +
   14.44 +#define _GNU_SOURCE
   14.45 +#include <getopt.h>
   14.46 +
   14.47 +#if !defined(__GNUC__) && !defined(__GNUG__)
   14.48 +#define __attribute__(arg) /* empty */
   14.49 +#endif
   14.50 +
   14.51 +#define KEY_ESCAPE '\x1B'
   14.52 +
   14.53 +/*
   14.54 + * Function prototypes
   14.55 + */
   14.56 +/* Utility functions */
   14.57 +static void usage(const char *);
   14.58 +static void version(void);
   14.59 +static void cleanup(void);
   14.60 +static void fail(const char *);
   14.61 +static int current_row(void);
   14.62 +static int lines(void);
   14.63 +static void print(const char *, ...) __attribute__((format(printf,1,2)));
   14.64 +static void attr_addstr(int attr, const char *str);
   14.65 +static void set_delay(char *value);
   14.66 +static void set_prompt(char *new_prompt, void (*func)(char *));
   14.67 +static int handle_key(int);
   14.68 +static int compare(unsigned long long, unsigned long long);
   14.69 +static int compare_domains(xenstat_domain **, xenstat_domain **);
   14.70 +static unsigned long long tot_net_bytes( xenstat_domain *, int);
   14.71 +
   14.72 +/* Field functions */
   14.73 +static int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2);
   14.74 +static void print_domid(xenstat_domain *domain);
   14.75 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2);
   14.76 +static void print_state(xenstat_domain *domain);
   14.77 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2);
   14.78 +static void print_cpu(xenstat_domain *domain);
   14.79 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2);
   14.80 +static void print_cpu_pct(xenstat_domain *domain);
   14.81 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2);
   14.82 +static void print_mem(xenstat_domain *domain);
   14.83 +static void print_mem_pct(xenstat_domain *domain);
   14.84 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2);
   14.85 +static void print_maxmem(xenstat_domain *domain);
   14.86 +static void print_max_pct(xenstat_domain *domain);
   14.87 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2);
   14.88 +static void print_vcpus(xenstat_domain *domain);
   14.89 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2);
   14.90 +static void print_nets(xenstat_domain *domain);
   14.91 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2);
   14.92 +static void print_net_tx(xenstat_domain *domain);
   14.93 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2);
   14.94 +static void print_net_rx(xenstat_domain *domain);
   14.95 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2);
   14.96 +static void print_ssid(xenstat_domain *domain);
   14.97 +
   14.98 +/* Section printing functions */
   14.99 +static void do_summary(void);
  14.100 +static void do_header(void);
  14.101 +static void do_bottom_line(void);
  14.102 +static void do_domain(xenstat_domain *);
  14.103 +static void do_vcpu(xenstat_domain *);
  14.104 +static void do_network(xenstat_domain *);
  14.105 +static void top(void);
  14.106 +
  14.107 +/* Field types */
  14.108 +typedef enum field_id {
  14.109 +	FIELD_DOMID,
  14.110 +	FIELD_STATE,
  14.111 +	FIELD_CPU,
  14.112 +	FIELD_CPU_PCT,
  14.113 +	FIELD_MEM,
  14.114 +	FIELD_MEM_PCT,
  14.115 +	FIELD_MAXMEM,
  14.116 +	FIELD_MAX_PCT,
  14.117 +	FIELD_VCPUS,
  14.118 +	FIELD_NETS,
  14.119 +	FIELD_NET_TX,
  14.120 +	FIELD_NET_RX,
  14.121 +	FIELD_SSID
  14.122 +} field_id;
  14.123 +
  14.124 +typedef struct field {
  14.125 +	field_id num;
  14.126 +	const char *header;
  14.127 +	unsigned int default_width;
  14.128 +	int (*compare)(xenstat_domain *domain1, xenstat_domain *domain2);
  14.129 +	void (*print)(xenstat_domain *domain);
  14.130 +} field;
  14.131 +
  14.132 +field fields[] = {
  14.133 +	{ FIELD_DOMID,   "DOMID",      5, compare_domid,   print_domid   },
  14.134 +	{ FIELD_STATE,   "STATE",      6, compare_state,   print_state   },
  14.135 +	{ FIELD_CPU,     "CPU(sec)",  10, compare_cpu,     print_cpu     },
  14.136 +	{ FIELD_CPU_PCT, "CPU(%)",     6, compare_cpu_pct, print_cpu_pct },
  14.137 +	{ FIELD_MEM,     "MEM(k)",    10, compare_mem,     print_mem     },
  14.138 +	{ FIELD_MEM_PCT, "MEM(%)",     6, compare_mem,     print_mem_pct },
  14.139 +	{ FIELD_MAXMEM,  "MAXMEM(k)", 10, compare_maxmem,  print_maxmem  },
  14.140 +	{ FIELD_MAX_PCT, "MAXMEM(%)",  9, compare_maxmem,  print_max_pct },
  14.141 +	{ FIELD_VCPUS,   "VCPUS",      5, compare_vcpus,   print_vcpus   },
  14.142 +	{ FIELD_NETS,    "NETS",       4, compare_nets,    print_nets    },
  14.143 +	{ FIELD_NET_TX,  "NETTX(k)",   8, compare_net_tx,  print_net_tx  },
  14.144 +	{ FIELD_NET_RX,  "NETRX(k)",   8, compare_net_rx,  print_net_rx  },
  14.145 +	{ FIELD_SSID,    "SSID",       4, compare_ssid,    print_ssid    }
  14.146 +};
  14.147 +
  14.148 +const unsigned int NUM_FIELDS = sizeof(fields)/sizeof(field);
  14.149 +
  14.150 +/* Globals */
  14.151 +struct timeval curtime, oldtime;
  14.152 +xenstat_handle *xhandle = NULL;
  14.153 +xenstat_node *prev_node = NULL;
  14.154 +xenstat_node *cur_node = NULL;
  14.155 +field_id sort_field = FIELD_DOMID;
  14.156 +unsigned int first_domain_index = 0;
  14.157 +unsigned int delay = 1;
  14.158 +int show_vcpus = 0;
  14.159 +int show_networks = 0;
  14.160 +int repeat_header = 0;
  14.161 +#define PROMPT_VAL_LEN 80
  14.162 +char *prompt = NULL;
  14.163 +char prompt_val[PROMPT_VAL_LEN];
  14.164 +int prompt_val_len = 0;
  14.165 +void (*prompt_complete_func)(char *);
  14.166 +
  14.167 +/*
  14.168 + * Function definitions
  14.169 + */
  14.170 +
  14.171 +/* Utility functions */
  14.172 +
  14.173 +/* Print usage message, using given program name */
  14.174 +static void usage(const char *program)
  14.175 +{
  14.176 +	printf("Usage: %s [OPTION]\n"
  14.177 +	       "Displays ongoing information about xen vm resources \n\n"
  14.178 +	       "-h, --help           display this help and exit\n"
  14.179 +	       "-V, --version        output version information and exit\n"
  14.180 +	       "-d, --delay=SECONDS  seconds between updates (default 1)\n"
  14.181 +	       "-n, --networks       output vif network data\n"
  14.182 +	       "-r, --repeat-header  repeat table header before each domain\n"
  14.183 +	       "-v, --vcpus          output vcpu data\n"
  14.184 +	       "\n" VM_TOP_BUGSTO,
  14.185 +	       program);
  14.186 +	return;
  14.187 +}
  14.188 +
  14.189 +/* Print program version information */
  14.190 +static void version(void)
  14.191 +{
  14.192 +	printf("vm-top " VM_TOP_VERSION "\n"
  14.193 +	       "Written by Judy Fischbach, David Hendricks, Josh Triplett\n"
  14.194 +	       "\n" VM_TOP_DISCLAIMER);
  14.195 +}
  14.196 +
  14.197 +/* Clean up any open resources */
  14.198 +static void cleanup(void)
  14.199 +{
  14.200 +	if(!isendwin())
  14.201 +		endwin();
  14.202 +	if(prev_node != NULL)
  14.203 +		xenstat_free_node(prev_node);
  14.204 +	if(cur_node != NULL)
  14.205 +		xenstat_free_node(cur_node);
  14.206 +	if(xhandle != NULL)
  14.207 +		xenstat_uninit(xhandle);
  14.208 +}
  14.209 +
  14.210 +/* Display the given message and gracefully exit */
  14.211 +static void fail(const char *str)
  14.212 +{
  14.213 +	if(!isendwin())
  14.214 +		endwin();
  14.215 +	fprintf(stderr, str);
  14.216 +	exit(1);
  14.217 +}
  14.218 +
  14.219 +/* Return the row containing the cursor. */
  14.220 +static int current_row(void)
  14.221 +{
  14.222 +	int y, x;
  14.223 +	getyx(stdscr, y, x);
  14.224 +	return y;
  14.225 +}
  14.226 +
  14.227 +/* Return the number of lines on the screen. */
  14.228 +static int lines(void)
  14.229 +{
  14.230 +	int y, x;
  14.231 +	getmaxyx(stdscr, y, x);
  14.232 +	return y;
  14.233 +}
  14.234 +
  14.235 +/* printf-style print function which calls printw, but only if the cursor is
  14.236 + * not on the last line. */
  14.237 +static void print(const char *fmt, ...)
  14.238 +{
  14.239 +	va_list args;
  14.240 +
  14.241 +	if(current_row() < lines()-1) {
  14.242 +		va_start(args, fmt);
  14.243 +		vw_printw(stdscr, fmt, args);
  14.244 +		va_end(args);
  14.245 +	}
  14.246 +}
  14.247 +
  14.248 +/* Print a string with the given attributes set. */
  14.249 +static void attr_addstr(int attr, const char *str)
  14.250 +{
  14.251 +	attron(attr);
  14.252 +	addstr(str);
  14.253 +	attroff(attr);
  14.254 +}
  14.255 +
  14.256 +/* Handle setting the delay from the user-supplied value in prompt_val */
  14.257 +static void set_delay(char *value)
  14.258 +{
  14.259 +	int new_delay;
  14.260 +	new_delay = atoi(prompt_val);
  14.261 +	if(new_delay > 0)
  14.262 +		delay = new_delay;
  14.263 +}
  14.264 +
  14.265 +/* Enable prompting mode with the given prompt string; call the given function
  14.266 + * when a value is available. */
  14.267 +static void set_prompt(char *new_prompt, void (*func)(char *))
  14.268 +{
  14.269 +	prompt = new_prompt;
  14.270 +	prompt_val[0] = '\0';
  14.271 +	prompt_val_len = 0;
  14.272 +	prompt_complete_func = func;
  14.273 +}
  14.274 +
  14.275 +/* Handle user input, return 0 if the program should quit, or 1 if not */
  14.276 +static int handle_key(int ch)
  14.277 +{
  14.278 +	if(prompt == NULL) {
  14.279 +		/* Not prompting for input; handle interactive commands */
  14.280 +		switch(ch) {
  14.281 +		case 'n': case 'N':
  14.282 +			show_networks ^= 1;
  14.283 +			break;
  14.284 +		case 'r': case 'R':
  14.285 +			repeat_header ^= 1;
  14.286 +			break;
  14.287 +		case 's': case 'S':
  14.288 +			sort_field = (sort_field + 1) % NUM_FIELDS;
  14.289 +			break;
  14.290 +		case 'v': case 'V':
  14.291 +			show_vcpus ^= 1;
  14.292 +			break;
  14.293 +		case KEY_DOWN:
  14.294 +			first_domain_index++;
  14.295 +			break;
  14.296 +		case KEY_UP:
  14.297 +			if(first_domain_index > 0)
  14.298 +				first_domain_index--;
  14.299 +			break;
  14.300 +		case 'd': case 'D':
  14.301 +			set_prompt("Delay(sec)", set_delay);
  14.302 +			break;
  14.303 +		case 'q': case 'Q': case KEY_ESCAPE:
  14.304 +			return 0;
  14.305 +		}
  14.306 +	} else {
  14.307 +		/* Prompting for input; handle line editing */
  14.308 +		switch(ch) {
  14.309 +		case '\r':
  14.310 +			prompt_complete_func(prompt_val);
  14.311 +			set_prompt(NULL, NULL);
  14.312 +			break;
  14.313 +		case KEY_ESCAPE:
  14.314 +			set_prompt(NULL, NULL);
  14.315 +			break;
  14.316 +		case KEY_BACKSPACE:
  14.317 +			if(prompt_val_len > 0)
  14.318 +				prompt_val[--prompt_val_len] = '\0';
  14.319 +		default:
  14.320 +			if((prompt_val_len+1) < PROMPT_VAL_LEN
  14.321 +			   && isprint(ch)) {
  14.322 +				prompt_val[prompt_val_len++] = (char)ch;
  14.323 +				prompt_val[prompt_val_len] = '\0';
  14.324 +			}
  14.325 +		}
  14.326 +	}
  14.327 +
  14.328 +	return 1;
  14.329 +}
  14.330 +
  14.331 +/* Compares two integers, returning -1,0,1 for <,=,> */
  14.332 +static int compare(unsigned long long i1, unsigned long long i2)
  14.333 +{
  14.334 +	if(i1 < i2)
  14.335 +		return -1;
  14.336 +	if(i1 > i2)
  14.337 +		return 1;
  14.338 +	return 0;
  14.339 +}
  14.340 +
  14.341 +/* Comparison function for use with qsort.  Compares two domains using the
  14.342 + * current sort field. */
  14.343 +static int compare_domains(xenstat_domain **domain1, xenstat_domain **domain2)
  14.344 +{
  14.345 +	return fields[sort_field].compare(*domain1, *domain2);
  14.346 +}
  14.347 +
  14.348 +/* Field functions */
  14.349 +
  14.350 +/* Compares domain ids of two domains, returning -1,0,1 for <,=,> */
  14.351 +int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2)
  14.352 +{
  14.353 +	return compare(xenstat_domain_id(domain1), xenstat_domain_id(domain2));
  14.354 +}
  14.355 +
  14.356 +/* Prints domain identification number */
  14.357 +void print_domid(xenstat_domain *domain)
  14.358 +{
  14.359 +	print("%5u", xenstat_domain_id(domain));
  14.360 +}
  14.361 +
  14.362 +struct {
  14.363 +	unsigned int (*get)(xenstat_domain *);
  14.364 +	char ch;
  14.365 +} state_funcs[] = {
  14.366 +	{ xenstat_domain_dying,    'd' },
  14.367 +	{ xenstat_domain_shutdown, 's' },
  14.368 +	{ xenstat_domain_blocked,  'b' },
  14.369 +	{ xenstat_domain_crashed,  'c' },
  14.370 +	{ xenstat_domain_paused,   'p' },
  14.371 +	{ xenstat_domain_running,  'r' }
  14.372 +};
  14.373 +const unsigned int NUM_STATES = sizeof(state_funcs)/sizeof(*state_funcs);
  14.374 +
  14.375 +/* Compare states of two domains, returning -1,0,1 for <,=,> */
  14.376 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2)
  14.377 +{
  14.378 +	unsigned int i, d1s, d2s;
  14.379 +	for(i = 0; i < NUM_STATES; i++) {
  14.380 +		d1s = state_funcs[i].get(domain1);
  14.381 +		d2s = state_funcs[i].get(domain2);
  14.382 +		if(d1s && !d2s)
  14.383 +			return -1;
  14.384 +		if(d2s && !d1s)
  14.385 +			return 1;
  14.386 +	}
  14.387 +	return 0;
  14.388 +}
  14.389 +
  14.390 +/* Prints domain state in abbreviated letter format */
  14.391 +static void print_state(xenstat_domain *domain)
  14.392 +{
  14.393 +	unsigned int i;
  14.394 +	for(i = 0; i < NUM_STATES; i++)
  14.395 +		print("%c", state_funcs[i].get(domain) ? state_funcs[i].ch
  14.396 +		                                       : '-');
  14.397 +}
  14.398 +
  14.399 +/* Compares cpu usage of two domains, returning -1,0,1 for <,=,> */
  14.400 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2)
  14.401 +{
  14.402 +	return -compare(xenstat_domain_cpu_ns(domain1),
  14.403 +			xenstat_domain_cpu_ns(domain2));
  14.404 +}
  14.405 +
  14.406 +/* Prints domain cpu usage in seconds */
  14.407 +static void print_cpu(xenstat_domain *domain)
  14.408 +{
  14.409 +	print("%10llu", xenstat_domain_cpu_ns(domain)/1000000000);
  14.410 +}
  14.411 +
  14.412 +/* Computes the CPU percentage used for a specified domain */
  14.413 +static double get_cpu_pct(xenstat_domain *domain)
  14.414 +{
  14.415 +	xenstat_domain *old_domain;
  14.416 +	double us_elapsed;
  14.417 +
  14.418 +	/* Can't calculate CPU percentage without a previous sample. */
  14.419 +	if(prev_node == NULL)
  14.420 +		return 0.0;
  14.421 +
  14.422 +	old_domain = xenstat_node_domain(prev_node, xenstat_domain_id(domain));
  14.423 +	if(old_domain == NULL)
  14.424 +		return 0.0;
  14.425 +
  14.426 +	/* Calculate the time elapsed in microseconds */
  14.427 +	us_elapsed = ((curtime.tv_sec-oldtime.tv_sec)*1000000.0
  14.428 +		      +(curtime.tv_usec - oldtime.tv_usec));
  14.429 +
  14.430 +	/* In the following, nanoseconds must be multiplied by 1000.0 to
  14.431 +	 * convert to microseconds, then divided by 100.0 to get a percentage,
  14.432 +	 * resulting in a multiplication by 10.0 */
  14.433 +	return ((xenstat_domain_cpu_ns(domain)
  14.434 +		 -xenstat_domain_cpu_ns(old_domain))/10.0)/us_elapsed;
  14.435 +}
  14.436 +
  14.437 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2)
  14.438 +{
  14.439 +	return -compare(get_cpu_pct(domain1), get_cpu_pct(domain2));
  14.440 +}
  14.441 +
  14.442 +/* Prints cpu percentage statistic */
  14.443 +static void print_cpu_pct(xenstat_domain *domain)
  14.444 +{
  14.445 +	print("%6.1f", get_cpu_pct(domain));
  14.446 +}
  14.447 +
  14.448 +/* Compares current memory of two domains, returning -1,0,1 for <,=,> */
  14.449 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2)
  14.450 +{
  14.451 +	return -compare(xenstat_domain_cur_mem(domain1),
  14.452 +	                xenstat_domain_cur_mem(domain2));
  14.453 +}
  14.454 +
  14.455 +/* Prints current memory statistic */
  14.456 +static void print_mem(xenstat_domain *domain)
  14.457 +{
  14.458 +	print("%10llu", xenstat_domain_cur_mem(domain)/1024);
  14.459 +}
  14.460 +
  14.461 +/* Prints memory percentage statistic, ratio of current domain memory to total
  14.462 + * node memory */
  14.463 +static void print_mem_pct(xenstat_domain *domain)
  14.464 +{
  14.465 +	print("%6.1f", (double)xenstat_domain_cur_mem(domain) /
  14.466 +	               (double)xenstat_node_tot_mem(cur_node) * 100);
  14.467 +}
  14.468 +
  14.469 +/* Compares maximum memory of two domains, returning -1,0,1 for <,=,> */
  14.470 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2)
  14.471 +{
  14.472 +	return -compare(xenstat_domain_max_mem(domain1),
  14.473 +	                xenstat_domain_max_mem(domain2));
  14.474 +}
  14.475 +
  14.476 +/* Prints maximum domain memory statistic in KB */
  14.477 +static void print_maxmem(xenstat_domain *domain)
  14.478 +{
  14.479 +	unsigned long long max_mem = xenstat_domain_max_mem(domain);
  14.480 +	if(max_mem == ((unsigned long long)-1))
  14.481 +		print("%10s", "no limit");
  14.482 +	else
  14.483 +		print("%10llu", max_mem/1024);
  14.484 +}
  14.485 +
  14.486 +/* Prints memory percentage statistic, ratio of current domain memory to total
  14.487 + * node memory */
  14.488 +static void print_max_pct(xenstat_domain *domain)
  14.489 +{
  14.490 +	if (xenstat_domain_max_mem(domain) == (unsigned long long)-1)
  14.491 +		print("%9s", "n/a");
  14.492 +	else
  14.493 +		print("%9.1f", (double)xenstat_domain_max_mem(domain) /
  14.494 +		               (double)xenstat_node_tot_mem(cur_node) * 100);
  14.495 +}
  14.496 +
  14.497 +/* Compares number of virtual CPUs of two domains, returning -1,0,1 for
  14.498 + * <,=,> */
  14.499 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2)
  14.500 +{
  14.501 +	return -compare(xenstat_domain_num_vcpus(domain1),
  14.502 +	                xenstat_domain_num_vcpus(domain2));
  14.503 +}
  14.504 +
  14.505 +/* Prints number of virtual CPUs statistic */
  14.506 +static void print_vcpus(xenstat_domain *domain)
  14.507 +{
  14.508 +	print("%5u", xenstat_domain_num_vcpus(domain));
  14.509 +}
  14.510 +
  14.511 +/* Compares number of virtual networks of two domains, returning -1,0,1 for
  14.512 + * <,=,> */
  14.513 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2)
  14.514 +{
  14.515 +	return -compare(xenstat_domain_num_networks(domain1),
  14.516 +	                xenstat_domain_num_networks(domain2));
  14.517 +}
  14.518 +
  14.519 +/* Prints number of virtual networks statistic */
  14.520 +static void print_nets(xenstat_domain *domain)
  14.521 +{
  14.522 +	print("%4u", xenstat_domain_num_networks(domain));
  14.523 +}
  14.524 +
  14.525 +/* Compares number of total network tx bytes of two domains, returning -1,0,1 for
  14.526 + * <,=,> */
  14.527 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2)
  14.528 +{
  14.529 +	return -compare(tot_net_bytes(domain1, FALSE),
  14.530 +	                tot_net_bytes(domain2, FALSE));
  14.531 +}
  14.532 +
  14.533 +/* Prints number of total network tx bytes statistic */
  14.534 +static void print_net_tx(xenstat_domain *domain)
  14.535 +{
  14.536 +	print("%8llu", tot_net_bytes(domain, FALSE)/1024);
  14.537 +}
  14.538 +
  14.539 +/* Compares number of total network rx bytes of two domains, returning -1,0,1 for
  14.540 + * <,=,> */
  14.541 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2)
  14.542 +{
  14.543 +	return -compare(tot_net_bytes(domain1, TRUE),
  14.544 +	                tot_net_bytes(domain2, TRUE));
  14.545 +}
  14.546 +
  14.547 +/* Prints number of total network rx bytes statistic */
  14.548 +static void print_net_rx(xenstat_domain *domain)
  14.549 +{
  14.550 +	print("%8llu", tot_net_bytes(domain, TRUE)/1024);
  14.551 +}
  14.552 +
  14.553 +/* Gets number of total network bytes statistic, if rx true, then rx bytes
  14.554 + * otherwise tx bytes
  14.555 + */
  14.556 +static unsigned long long tot_net_bytes(xenstat_domain *domain, int rx_flag)
  14.557 +{
  14.558 +	int i = 0;
  14.559 +	xenstat_network *network;
  14.560 +	unsigned num_networks = 0;
  14.561 +        unsigned long long total = 0;
  14.562 +
  14.563 +	/* How many networks? */
  14.564 +	num_networks = xenstat_domain_num_networks(domain);
  14.565 +
  14.566 +	/* Dump information for each network */
  14.567 +	for (i=0; i < num_networks; i++) {
  14.568 +		/* Next get the network information */
  14.569 +		network = xenstat_domain_network(domain,i);
  14.570 +                if (rx_flag) 
  14.571 +			total += xenstat_network_rbytes(network);
  14.572 +                else 
  14.573 +			total += xenstat_network_tbytes(network);
  14.574 +	}
  14.575 +        return (total);
  14.576 +}
  14.577 +
  14.578 +/* Compares security id (ssid) of two domains, returning -1,0,1 for <,=,> */
  14.579 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2)
  14.580 +{
  14.581 +	return compare(xenstat_domain_ssid(domain1),
  14.582 +		       xenstat_domain_ssid(domain2));
  14.583 +}
  14.584 +
  14.585 +/* Prints ssid statistic */
  14.586 +static void print_ssid(xenstat_domain *domain)
  14.587 +{
  14.588 +	print("%4u", xenstat_domain_ssid(domain));
  14.589 +}
  14.590 +
  14.591 +/* Section printing functions */
  14.592 +/* Prints three line summary header */
  14.593 +void do_summary(void)
  14.594 +{
  14.595 +#define TIME_STR_LEN 9
  14.596 +	const char *TIME_STR_FORMAT = "%H:%M:%S";
  14.597 +	char time_str[TIME_STR_LEN];
  14.598 +	unsigned run = 0, block = 0, pause = 0,
  14.599 +	         crash = 0, dying = 0, shutdown = 0;
  14.600 +	unsigned i, num_domains = 0;
  14.601 +	unsigned long long used = 0;
  14.602 +	xenstat_domain *domain;
  14.603 +
  14.604 +	/* Print program name, current time, and number of domains */
  14.605 +	strftime(time_str, TIME_STR_LEN, TIME_STR_FORMAT,
  14.606 +	         localtime(&curtime.tv_sec));
  14.607 +	num_domains = xenstat_node_num_domains(cur_node);
  14.608 +	print("vm-top - %s\n", time_str);
  14.609 +
  14.610 +	/* Tabulate what states domains are in for summary */
  14.611 +	for (i=0; i < num_domains; i++) {
  14.612 +		domain = xenstat_node_domain_by_index(cur_node,i);
  14.613 +		if (xenstat_domain_running(domain)) run++;
  14.614 +		else if (xenstat_domain_blocked(domain)) block++;
  14.615 +		else if (xenstat_domain_paused(domain)) pause++;
  14.616 +		else if (xenstat_domain_shutdown(domain)) shutdown++;
  14.617 +		else if (xenstat_domain_crashed(domain)) crash++;
  14.618 +		else if (xenstat_domain_dying(domain)) dying++;
  14.619 +	}
  14.620 +
  14.621 +	print("%u domains: %u running, %u blocked, %u paused, "
  14.622 +	      "%u crashed, %u dying, %u shutdown \n",
  14.623 +	      num_domains, run, block, pause, crash, dying, shutdown);
  14.624 +
  14.625 +	used = xenstat_node_tot_mem(cur_node)-xenstat_node_free_mem(cur_node);
  14.626 +
  14.627 +	/* Dump node memory and cpu information */
  14.628 +	print("Mem: %lluk total, %lluk used, %lluk free    "
  14.629 +	      "CPUs: %u @ %lluMHz\n",
  14.630 +	      xenstat_node_tot_mem(cur_node)/1024, used/1024,
  14.631 +	      xenstat_node_free_mem(cur_node)/1024,
  14.632 +	      xenstat_node_num_cpus(cur_node),
  14.633 +	      xenstat_node_cpu_hz(cur_node)/1000000);
  14.634 +}
  14.635 +
  14.636 +/* Display top portion of vm-top */
  14.637 +void do_header(void)
  14.638 +{
  14.639 +	field_id i;
  14.640 +
  14.641 +	/* Turn on REVERSE highlight attribute for headings */
  14.642 +	attron(A_REVERSE);
  14.643 +	for(i = 0; i < NUM_FIELDS; i++) {
  14.644 +		if(i != 0)
  14.645 +			print(" ");
  14.646 +		/* The BOLD attribute is turned on for the sort column */
  14.647 +		if(i == sort_field)
  14.648 +			attron(A_BOLD);
  14.649 +		print("%*s", fields[i].default_width, fields[i].header);
  14.650 +		if(i == sort_field)
  14.651 +			attroff(A_BOLD);
  14.652 +	}
  14.653 +	attroff(A_REVERSE);
  14.654 +	print("\n");
  14.655 +}
  14.656 +
  14.657 +/* Displays bottom portion of vm-top, interactive options
  14.658 + * N toggles network information display, V toggles CPU information
  14.659 + * display, S toggles sort order of information (ascending/descending),
  14.660 + * R toggles whether header is repeated for each domain
  14.661 + */
  14.662 +void do_bottom_line(void)
  14.663 +{
  14.664 +	move(lines()-1, 2);
  14.665 +
  14.666 +	if (prompt != NULL) {
  14.667 +		printw("%s: %s", prompt, prompt_val);
  14.668 +	} else {
  14.669 +		addch(A_REVERSE | 'D'); addstr("elay  ");
  14.670 +
  14.671 +		/* network */
  14.672 +		addch(A_REVERSE | 'N');
  14.673 +		attr_addstr(show_networks ? COLOR_PAIR(1) : 0, "etworks");
  14.674 +		addstr("  ");
  14.675 +
  14.676 +		/* vcpus */
  14.677 +		addch(A_REVERSE | 'V');
  14.678 +		attr_addstr(show_vcpus ? COLOR_PAIR(1) : 0, "CPUs");
  14.679 +		addstr("  ");
  14.680 +
  14.681 +		/* repeat */
  14.682 +		addch(A_REVERSE | 'R');
  14.683 +		attr_addstr(repeat_header ? COLOR_PAIR(1) : 0, "epeat header");
  14.684 +		addstr("  ");
  14.685 +
  14.686 +		/* sort order */
  14.687 +		addch(A_REVERSE | 'S'); addstr("ort order  ");
  14.688 +
  14.689 +		addch(A_REVERSE | 'Q'); addstr("uit  ");
  14.690 +	}
  14.691 +}
  14.692 +
  14.693 +/* Prints Domain information */
  14.694 +void do_domain(xenstat_domain *domain)
  14.695 +{
  14.696 +	unsigned int i;
  14.697 +	for(i = 0; i < NUM_FIELDS; i++) {
  14.698 +		if(i != 0)
  14.699 +			print(" ");
  14.700 +		if(i == sort_field)
  14.701 +			attron(A_BOLD);
  14.702 +		fields[i].print(domain);
  14.703 +		if(i == sort_field)
  14.704 +			attroff(A_BOLD);
  14.705 +	}
  14.706 +	print("\n");
  14.707 +}
  14.708 +
  14.709 +/* Output all vcpu information */
  14.710 +void do_vcpu(xenstat_domain *domain)
  14.711 +{
  14.712 +	int i = 0;
  14.713 +	unsigned num_vcpus = 0;
  14.714 +	xenstat_vcpu *vcpu;
  14.715 +
  14.716 +	print("VCPUs(sec): ");
  14.717 +
  14.718 +	num_vcpus = xenstat_domain_num_vcpus(domain);
  14.719 +
  14.720 +	/* for all vcpus dump out values */
  14.721 +	for (i=0; i< num_vcpus; i++) {
  14.722 +		vcpu = xenstat_domain_vcpu(domain,i);
  14.723 +
  14.724 +		if (i != 0 && (i%5)==0)
  14.725 +			print("\n        ");
  14.726 +		print(" %2u: %10llus", i, xenstat_vcpu_ns(vcpu)/1000000000);
  14.727 +	}
  14.728 +	print("\n");
  14.729 +}
  14.730 +
  14.731 +/* Output all network information */
  14.732 +void do_network(xenstat_domain *domain)
  14.733 +{
  14.734 +	int i = 0;
  14.735 +	xenstat_network *network;
  14.736 +	unsigned num_networks = 0;
  14.737 +
  14.738 +	/* How many networks? */
  14.739 +	num_networks = xenstat_domain_num_networks(domain);
  14.740 +
  14.741 +	/* Dump information for each network */
  14.742 +	for (i=0; i < num_networks; i++) {
  14.743 +		/* Next get the network information */
  14.744 +		network = xenstat_domain_network(domain,i);
  14.745 +
  14.746 +		print("Net%d RX: %8llubytes %8llupkts %8lluerr %8lludrop  ",
  14.747 +		      i,
  14.748 +		      xenstat_network_rbytes(network),
  14.749 +		      xenstat_network_rpackets(network),
  14.750 +		      xenstat_network_rerrs(network),
  14.751 +		      xenstat_network_rdrop(network));
  14.752 +
  14.753 +		print("TX: %8llubytes %8llupkts %8lluerr %8lludrop\n",
  14.754 +		      xenstat_network_tbytes(network),
  14.755 +		      xenstat_network_tpackets(network),
  14.756 +		      xenstat_network_terrs(network),
  14.757 +		      xenstat_network_tdrop(network));
  14.758 +	}
  14.759 +}
  14.760 +
  14.761 +static void top(void)
  14.762 +{
  14.763 +	xenstat_domain **domains;
  14.764 +	unsigned int i, num_domains = 0;
  14.765 +
  14.766 +	/* Now get the node information */
  14.767 +	if (prev_node != NULL)
  14.768 +		xenstat_free_node(prev_node);
  14.769 +	prev_node = cur_node;
  14.770 +	cur_node = xenstat_get_node(xhandle, XENSTAT_ALL);
  14.771 +	if (cur_node == NULL)
  14.772 +		fail("Failed to retrieve statistics from libxenstat\n");
  14.773 +
  14.774 +	/* dump summary top information */
  14.775 +	do_summary();
  14.776 +
  14.777 +	/* Count the number of domains for which to report data */
  14.778 +	num_domains = xenstat_node_num_domains(cur_node);
  14.779 +
  14.780 +	domains = malloc(num_domains*sizeof(xenstat_domain *));
  14.781 +	if(domains == NULL)
  14.782 +		fail("Failed to allocate memory\n");
  14.783 +
  14.784 +	for (i=0; i < num_domains; i++)
  14.785 +		domains[i] = xenstat_node_domain_by_index(cur_node, i);
  14.786 +
  14.787 +	/* Sort */
  14.788 +	qsort(domains, num_domains, sizeof(xenstat_domain *),
  14.789 +	      (int(*)(const void *, const void *))compare_domains);
  14.790 +
  14.791 +	if(first_domain_index >= num_domains)
  14.792 +		first_domain_index = num_domains-1;
  14.793 +
  14.794 +	for (i = first_domain_index; i < num_domains; i++) {
  14.795 +		if(current_row() == lines()-1)
  14.796 +			break;
  14.797 +		if (i == first_domain_index || repeat_header)
  14.798 +			do_header();
  14.799 +		do_domain(domains[i]);
  14.800 +		if (show_vcpus)
  14.801 +			do_vcpu(domains[i]);
  14.802 +		if (show_networks)
  14.803 +			do_network(domains[i]);
  14.804 +	}
  14.805 +
  14.806 +	do_bottom_line();
  14.807 +}
  14.808 +
  14.809 +int main(int argc, char **argv)
  14.810 +{
  14.811 +	int opt, optind = 0;
  14.812 +	int ch = ERR;
  14.813 +
  14.814 +	struct option lopts[] = {
  14.815 +		{ "help",          no_argument,       NULL, 'h' },
  14.816 +		{ "version",       no_argument,       NULL, 'V' },
  14.817 +		{ "networks",      no_argument,       NULL, 'n' },
  14.818 +		{ "repeat-header", no_argument,       NULL, 'r' },
  14.819 +		{ "vcpus",         no_argument,       NULL, 'v' },
  14.820 +		{ "delay",         required_argument, NULL, 'd' },
  14.821 +		{ 0, 0, 0, 0 },
  14.822 +	};
  14.823 +	const char *sopts = "hVbnvd:";
  14.824 +
  14.825 +	if (atexit(cleanup) != 0)
  14.826 +		fail("Failed to install cleanup handler.\n");
  14.827 +
  14.828 +	while ((opt = getopt_long(argc, argv, sopts, lopts, &optind)) != -1) {
  14.829 +		switch (opt) {
  14.830 +		case 'h':
  14.831 +		case '?':
  14.832 +		default:
  14.833 +			usage(argv[0]);
  14.834 +			exit(0);
  14.835 +		case 'V':
  14.836 +			version();
  14.837 +			exit(0);
  14.838 +		case 'n':
  14.839 +			show_networks = 1;
  14.840 +			break;
  14.841 +		case 'r':
  14.842 +			repeat_header = 1;
  14.843 +			break;
  14.844 +		case 'v':
  14.845 +			show_vcpus = 1;
  14.846 +			break;
  14.847 +		case 'd':
  14.848 +			delay = atoi(optarg);
  14.849 +			break;
  14.850 +		}
  14.851 +	}
  14.852 +
  14.853 +	/* Get xenstat handle */
  14.854 +	xhandle = xenstat_init();
  14.855 +	if (xhandle == NULL)
  14.856 +		fail("Failed to initialize xenstat library\n");
  14.857 +
  14.858 +	/* Begin curses stuff */
  14.859 +	initscr();
  14.860 +	start_color();
  14.861 +	cbreak();
  14.862 +	noecho();
  14.863 +	nonl();
  14.864 +	keypad(stdscr, TRUE);
  14.865 +	halfdelay(5);
  14.866 +	use_default_colors();
  14.867 +	init_pair(1, -1, COLOR_YELLOW);
  14.868 +
  14.869 +	do {
  14.870 +		gettimeofday(&curtime, NULL);
  14.871 +		if(ch != ERR || (curtime.tv_sec - oldtime.tv_sec) >= delay) {
  14.872 +			clear();
  14.873 +			top();
  14.874 +			oldtime = curtime;
  14.875 +			refresh();
  14.876 +		}
  14.877 +		ch = getch();
  14.878 +	} while (handle_key(ch));
  14.879 +
  14.880 +	/* Cleanup occurs in cleanup(), so no work to do here. */
  14.881 +
  14.882 +	return 0;
  14.883 +}