]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
vmmemctl: switch to using OS-specific methods to format stats
authorVMware, Inc <>
Thu, 17 Jun 2010 22:02:19 +0000 (15:02 -0700)
committerMarcelo Vanzin <mvanzin@vmware.com>
Thu, 17 Jun 2010 22:02:19 +0000 (15:02 -0700)
The common routine to output ballooning statistics does not work
for Solraris (which uses kstats) and is awkward on Linux/BSD,
where we need to allocate additional memory, introduce a wrappers
around snprintf and so on. Let's leave it to every OS to generate
stats in the most natural way for that particular OS.

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/modules/freebsd/vmmemctl/os.c
open-vm-tools/modules/shared/vmmemctl/os.h
open-vm-tools/modules/shared/vmmemctl/vmballoon.c
open-vm-tools/modules/shared/vmmemctl/vmballoon.h
open-vm-tools/modules/solaris/vmmemctl/os.c
open-vm-tools/modules/solaris/vmmemctl/vmballoon_kstats.c

index bc6158ca154225503ca9da8756bcf654c61f4314..0bf8bf6a6b0838bad015d7b7e8b02d0b28092783 100644 (file)
@@ -75,7 +75,6 @@ typedef struct {
 
 typedef struct {
    /* registered state */
-   OSStatusHandler *handler;
    const char *name_verbose;
    const char *name;
 } os_status;
@@ -217,39 +216,6 @@ static __inline__ unsigned long os_ffz(unsigned long word)
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *
- * OS_Snprintf --
- *
- *      Print a string into a bounded memory location.
- *
- * Results:
- *      Number of character printed including trailing \0.
- *
- * Side effects:
- *      None
- *
- *-----------------------------------------------------------------------------
- */
-
-int
-OS_Snprintf(char *buf,          // OUT
-            size_t size,        // IN
-            const char *format, // IN
-            ...)                // IN
-{
-   int result;
-   va_list args;
-
-   va_start(args, format);
-   result = vsnprintf(buf, size, format, args);
-   va_end(args);
-
-   return result;
-}
-
-
 /*
  *-----------------------------------------------------------------------------
  *
@@ -649,8 +615,7 @@ OS_Yield(void)
 
 Bool
 OS_Init(const char *name,         // IN
-        const char *nameVerbose,  // IN
-        OSStatusHandler *handler) // IN
+        const char *nameVerbose)  // IN
 {
    os_state *state = &global_state;
    os_pmap *pmap = &state->pmap;
@@ -668,7 +633,6 @@ OS_Init(const char *name,         // IN
    callout_handle_init(&state->timer.callout_handle);
 
    /* initialize status state */
-   state->status.handler = handler;
    state->status.name = name;
    state->status.name_verbose = nameVerbose;
 
@@ -774,12 +738,54 @@ static struct sysctl_oid *oid;
 static int
 vmmemctl_sysctl(SYSCTL_HANDLER_ARGS)
 {
-   char stats[PAGE_SIZE];
-   size_t len;
-
-   len = 1 + global_state.status.handler(stats, PAGE_SIZE);
-
-   return SYSCTL_OUT(req, stats, len);
+   char buf[PAGE_SIZE];
+   size_t len = 0;
+   const BalloonStats *stats = Balloon_GetStats();
+
+   /* format size info */
+   len += snprintf(buf + len, sizeof(buf) - len,
+                   "target:             %8d pages\n"
+                   "current:            %8d pages\n",
+                   stats->nPagesTarget,
+                   stats->nPages);
+
+   /* format rate info */
+   len += snprintf(buf + len, sizeof(buf) - len,
+                   "rateNoSleepAlloc:   %8d pages/sec\n"
+                   "rateSleepAlloc:     %8d pages/sec\n"
+                   "rateFree:           %8d pages/sec\n",
+                   stats->rateNoSleepAlloc,
+                   stats->rateAlloc,
+                   stats->rateFree);
+
+   len += snprintf(buf + len, sizeof(buf) - len,
+                   "\n"
+                   "timer:              %8u\n"
+                   "start:              %8u (%4u failed)\n"
+                   "guestType:          %8u (%4u failed)\n"
+                   "lock:               %8u (%4u failed)\n"
+                   "unlock:             %8u (%4u failed)\n"
+                   "target:             %8u (%4u failed)\n"
+                   "primNoSleepAlloc:   %8u (%4u failed)\n"
+                   "primCanSleepAlloc:  %8u (%4u failed)\n"
+                   "primFree:           %8u\n"
+                   "errAlloc:           %8u\n"
+                   "errFree:            %8u\n",
+                   stats->timer,
+                   stats->start, stats->startFail,
+                   stats->guestType, stats->guestTypeFail,
+                   stats->lock,  stats->lockFail,
+                   stats->unlock, stats->unlockFail,
+                   stats->target, stats->targetFail,
+                   stats->primAlloc[BALLOON_PAGE_ALLOC_NOSLEEP],
+                   stats->primAllocFail[BALLOON_PAGE_ALLOC_NOSLEEP],
+                   stats->primAlloc[BALLOON_PAGE_ALLOC_CANSLEEP],
+                   stats->primAllocFail[BALLOON_PAGE_ALLOC_CANSLEEP],
+                   stats->primFree,
+                   stats->primErrorPageAlloc,
+                   stats->primErrorPageFree);
+
+   return SYSCTL_OUT(req, buf, len + 1);
 }
 
 
index 3125da7a1f9351d6fcf959262df7ad9a41dbb212..43a89d28ea91e22f1c780147d14d4d013522c3c0 100644 (file)
@@ -85,14 +85,12 @@ typedef uintptr_t PageHandle;
  */
 
 extern Bool OS_Init(const char *name,
-                    const char *nameVerbose,
-                    OSStatusHandler *handler);
+                    const char *nameVerbose);
 extern void OS_Cleanup(void);
 extern BalloonGuest OS_Identity(void);
 
 extern void OS_MemZero(void *ptr, size_t size);
 extern void OS_MemCopy(void *dest, const void *src, size_t size);
-extern int  OS_Snprintf(char *buf, size_t size, const char *format, ...);
 
 extern void *OS_Malloc(size_t size);
 extern void OS_Free(void *ptr, size_t size);
index ff6d03cbdce59037a7f85c69e9abc1e3d66fbfd8..35206e8b15c45df9426a51de4698660364f26fbb 100644 (file)
@@ -86,7 +86,6 @@ extern "C" {
 #define BALLOON_DEBUG_VERBOSE   0
 
 #define BALLOON_STATS
-#define BALLOON_STATS_PROCFS
 
 /*
  * Includes
@@ -219,81 +218,6 @@ static int BalloonMonitorUnlockPage(Balloon *b, PageHandle handle);
 #define STATS_INC(stat)
 #endif
 
-/*
- *----------------------------------------------------------------------
- *
- * BalloonProcRead --
- *
- *      Ballon driver status reporting routine.  Note that this is only
- *      used for Linux.
- *
- * Results:
- *      Writes ASCII status information into "buf".
- *      Returns number of bytes written.
- *
- * Side effects:
- *      None.
- *
- *----------------------------------------------------------------------
- */
-
-static int
-BalloonProcRead(char *buf,   // OUT
-                size_t size) // IN
-{
-   int len = 0;
-   BalloonStats stats;
-
-   Balloon_GetStats(&stats);
-
-   /* format size info */
-   len += OS_Snprintf(buf + len, size - len,
-                     "target:             %8d pages\n"
-                     "current:            %8d pages\n",
-                     stats.nPagesTarget,
-                     stats.nPages);
-
-   /* format rate info */
-   len += OS_Snprintf(buf + len, size - len,
-                     "rateNoSleepAlloc:   %8d pages/sec\n"
-                     "rateSleepAlloc:     %8d pages/sec\n"
-                     "rateFree:           %8d pages/sec\n",
-                     BALLOON_NOSLEEP_ALLOC_MAX,
-                     stats.rateAlloc,
-                     stats.rateFree);
-
-#ifdef BALLOON_STATS_PROCFS
-   len += OS_Snprintf(buf + len, size - len,
-                     "\n"
-                     "timer:              %8u\n"
-                     "start:              %8u (%4u failed)\n"
-                     "guestType:          %8u (%4u failed)\n"
-                     "lock:               %8u (%4u failed)\n"
-                     "unlock:             %8u (%4u failed)\n"
-                     "target:             %8u (%4u failed)\n"
-                     "primNoSleepAlloc:   %8u (%4u failed)\n"
-                     "primCanSleepAlloc:  %8u (%4u failed)\n"
-                     "primFree:           %8u\n"
-                     "errAlloc:           %8u\n"
-                     "errFree:            %8u\n",
-                     stats.timer,
-                     stats.start, stats.startFail,
-                     stats.guestType, stats.guestTypeFail,
-                     stats.lock,  stats.lockFail,
-                     stats.unlock, stats.unlockFail,
-                     stats.target, stats.targetFail,
-                     stats.primAlloc[BALLOON_PAGE_ALLOC_NOSLEEP],
-                     stats.primAllocFail[BALLOON_PAGE_ALLOC_NOSLEEP],
-                     stats.primAlloc[BALLOON_PAGE_ALLOC_CANSLEEP],
-                     stats.primAllocFail[BALLOON_PAGE_ALLOC_CANSLEEP],
-                     stats.primFree,
-                     stats.primErrorPageAlloc,
-                     stats.primErrorPageFree);
-#endif
-
-   return len;
-}
-
 
 /*
  *----------------------------------------------------------------------
@@ -313,15 +237,11 @@ BalloonProcRead(char *buf,   // OUT
  *----------------------------------------------------------------------
  */
 
-void
-Balloon_GetStats(BalloonStats *stats) // OUT
+const BalloonStats *
+Balloon_GetStats(void)
 {
    Balloon *b = &globalBalloon;
-
-   /*
-    * Copy statistics out of global structure.
-    */
-   OS_MemCopy(stats, &b->stats, sizeof *stats);
+   BalloonStats *stats = &b->stats;
 
    /*
     * Fill in additional information about size and rates, which is
@@ -329,8 +249,11 @@ Balloon_GetStats(BalloonStats *stats) // OUT
     */
    stats->nPages = b->nPages;
    stats->nPagesTarget = b->nPagesTarget;
+   stats->rateNoSleepAlloc = BALLOON_NOSLEEP_ALLOC_MAX;
    stats->rateAlloc = b->rateAlloc;
    stats->rateFree = b->rateFree;
+
+   return stats;
 }
 
 
@@ -1397,7 +1320,7 @@ Balloon_ModuleInit(void)
    Balloon *b = &globalBalloon;
 
    /* os-specific initialization */
-   if (!OS_Init(BALLOON_NAME, BALLOON_NAME_VERBOSE, BalloonProcRead)) {
+   if (!OS_Init(BALLOON_NAME, BALLOON_NAME_VERBOSE)) {
       return BALLOON_FAILURE;
    }
 
index 861329e9a5dc7958a5bc16264862562bbeffcaad..6917c73b913612d9b79ba61fadec257768143d23 100644 (file)
@@ -86,6 +86,7 @@ typedef struct {
    uint32 nPagesTarget;
 
    /* adjustment rates */
+   uint32 rateNoSleepAlloc;
    uint32 rateAlloc;
    uint32 rateFree;
 
@@ -119,6 +120,6 @@ typedef struct {
 extern int  Balloon_ModuleInit(void);
 extern void Balloon_ModuleCleanup(void);
 
-extern void Balloon_GetStats(BalloonStats *stats);
+extern const BalloonStats *Balloon_GetStats(void);
 
 #endif /* VMBALLOON_H */
index b3d680d606dafcce7c1cadfc75f1d28d7eda8d3d..6236bc67626e4344486dd2e9bbe45495f17fd1fd 100644 (file)
@@ -200,38 +200,6 @@ OS_MemCopy(void *dest,      // OUT
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *
- * OS_Snprintf --
- *
- *      Print a string into a bounded memory location.
- *
- * Results:
- *      Number of character printed including trailing \0.
- *
- * Side effects:
- *      None
- *
- *-----------------------------------------------------------------------------
- */
-
-int
-OS_Snprintf(char *buf,          // OUT
-            size_t size,        // IN
-            const char *format, // IN
-            ...)                // IN
-{
-   ASSERT(0);
-   /*
-    * XXX disabled because the varargs header file doesn't seem to
-    * work in the current (gcc 2.95.3) cross-compiler environment.
-    * Not used for Solaris anyway.
-    */
-   return 0;
-}
-
-
 /*
  *-----------------------------------------------------------------------------
  *
@@ -609,8 +577,7 @@ OS_Yield(void)
 
 Bool
 OS_Init(const char *name,         // IN
-        const char *nameVerbose,  // IN
-        OSStatusHandler *handler) // IN
+        const char *nameVerbose)  // IN
 {
    os_state *state = &global_state;
    static int initialized = 0;
index 4b191823677e05fd00e0d3e2987e5a8606f78688..1c6063a88b54519bf1a6030484e48b33444f324b 100644 (file)
@@ -25,9 +25,6 @@
  * Compile-Time Options
  */
 
-#define        BALLOON_STATS
-#define        BALLOON_STATS_PROCFS
-
 #include <sys/types.h>
 #include <sys/kstat.h>
 #include <sys/errno.h>
@@ -84,41 +81,41 @@ static int
 BalloonKstatUpdate(kstat_t *ksp, int rw)
 {
    int i;
-   BalloonStats stats;
+   const BalloonStats *stats;
    BalloonKstats *bkp = ksp->ks_data;
 
    if (rw == KSTAT_WRITE)
       return (EACCES);
 
-   Balloon_GetStats(&stats);
+   stats = Balloon_GetStats();
 
    /* size info */
-   bkp->nPagesTarget.value.ui32 = stats.nPagesTarget;
-   bkp->nPages.value.ui32 = stats.nPages;
+   bkp->nPagesTarget.value.ui32 = stats->nPagesTarget;
+   bkp->nPages.value.ui32 = stats->nPages;
 
    /* rate info */
-   bkp->rateAlloc.value.ui32 = stats.rateAlloc;
-   bkp->rateFree.value.ui32 = stats.rateFree;
+   bkp->rateAlloc.value.ui32 = stats->rateAlloc;
+   bkp->rateFree.value.ui32 = stats->rateFree;
 
    /* statistics */
-   bkp->timer.value.ui32 = stats.timer;
-   bkp->start.value.ui32 = stats.start;
-   bkp->startFail.value.ui32 = stats.startFail;
-   bkp->guestType.value.ui32 = stats.guestType;
-   bkp->guestTypeFail.value.ui32 = stats.guestTypeFail;
-   bkp->lock.value.ui32 = stats.lock;
-   bkp->lockFail.value.ui32 = stats.lockFail;
-   bkp->unlock.value.ui32 = stats.unlock;
-   bkp->unlockFail.value.ui32 = stats.unlockFail;
-   bkp->target.value.ui32 = stats.target;
-   bkp->targetFail.value.ui32 = stats.targetFail;
+   bkp->timer.value.ui32 = stats->timer;
+   bkp->start.value.ui32 = stats->start;
+   bkp->startFail.value.ui32 = stats->startFail;
+   bkp->guestType.value.ui32 = stats->guestType;
+   bkp->guestTypeFail.value.ui32 = stats->guestTypeFail;
+   bkp->lock.value.ui32 = stats->lock;
+   bkp->lockFail.value.ui32 = stats->lockFail;
+   bkp->unlock.value.ui32 = stats->unlock;
+   bkp->unlockFail.value.ui32 = stats->unlockFail;
+   bkp->target.value.ui32 = stats->target;
+   bkp->targetFail.value.ui32 = stats->targetFail;
    for (i = 0; i < BALLOON_PAGE_ALLOC_TYPES_NR; i++) {
-      bkp->primAlloc[i].value.ui32 = stats.primAlloc[i];
-      bkp->primAllocFail[i].value.ui32 = stats.primAllocFail[i];
+      bkp->primAlloc[i].value.ui32 = stats->primAlloc[i];
+      bkp->primAllocFail[i].value.ui32 = stats->primAllocFail[i];
    }
-   bkp->primFree.value.ui32 = stats.primFree;
-   bkp->primErrorPageAlloc.value.ui32 = stats.primErrorPageAlloc;
-   bkp->primErrorPageFree.value.ui32 = stats.primErrorPageFree;
+   bkp->primFree.value.ui32 = stats->primFree;
+   bkp->primErrorPageAlloc.value.ui32 = stats->primErrorPageAlloc;
+   bkp->primErrorPageFree.value.ui32 = stats->primErrorPageFree;
 
    return 0;
 }