From 53ea21040aa51f2ff0fa69a17a1b304b6b6331a3 Mon Sep 17 00:00:00 2001 From: Mark Wong Date: Wed, 10 Sep 2008 20:39:18 -0700 Subject: [PATCH] Turned skip_token() into a macro and hopefully simplified its use. --- pg_common.h | 12 +++++++++++ pg_cputime.c | 18 +---------------- pg_loadavg.c | 17 +--------------- pg_memusage.c | 46 ++++++++---------------------------------- pg_proctab.c | 55 ++++++++++++++++++--------------------------------- 5 files changed, 41 insertions(+), 107 deletions(-) diff --git a/pg_common.h b/pg_common.h index dd6f652..52c2db2 100644 --- a/pg_common.h +++ b/pg_common.h @@ -14,6 +14,7 @@ PG_MODULE_MAGIC; #define INTEGER_LEN 10 #ifdef __linux__ +#include #include #define PROCFS "/proc" @@ -28,6 +29,17 @@ PG_MODULE_MAGIC; strncpy(value, p, length); \ value[length] = '\0'; \ p = q + 1; + +#define SKIP_TOKEN(p) \ + /* Skipping leading white space. */ \ + while (isspace(*p)) \ + p++; \ + /* Skip token. */ \ + while (*p && !isspace(*p)) \ + p++; \ + /* Skipping trailing white space. */ \ + while (isspace(*p)) \ + p++; #endif /* __linux__ */ #endif /* _PG_COMMON_H_ */ diff --git a/pg_cputime.c b/pg_cputime.c index a806f2a..7fb5f41 100644 --- a/pg_cputime.c +++ b/pg_cputime.c @@ -15,20 +15,6 @@ #include #include "pg_common.h" -#ifdef __linux__ -static inline char *skip_token(const char *); - -static inline char * -skip_token(const char *p) -{ - while (isspace(*p)) - p++; - while (*p && !isspace(*p)) - p++; - return (char *) p; -} -#endif /* __linux__ */ - enum loadavg {i_user, i_nice, i_system, i_idle, i_iowait}; int get_cputime(char **); @@ -148,9 +134,7 @@ get_cputime(char **values) p = buffer; - p = skip_token(p); /* skip cpu */ - ++p; - ++p; + SKIP_TOKEN(p); /* skip cpu */ /* user */ GET_NEXT_VALUE(p, q, values[i_user], length, "user not found", ' '); diff --git a/pg_loadavg.c b/pg_loadavg.c index fe3dc9e..867aa1f 100644 --- a/pg_loadavg.c +++ b/pg_loadavg.c @@ -15,20 +15,6 @@ #include #include "pg_common.h" -#ifdef __linux__ -static inline char *skip_token(const char *); - -static inline char * -skip_token(const char *p) -{ - while (isspace(*p)) - p++; - while (*p && !isspace(*p)) - p++; - return (char *) p; -} -#endif /* __linux__ */ - enum loadavg {i_load1, i_load5, i_load15, i_last_pid}; int get_loadavg(char **); @@ -157,8 +143,7 @@ get_loadavg(char **values) /* load15 */ GET_NEXT_VALUE(p, q, values[i_load15], length, "load15 not found", ' '); - p = skip_token(p); /* skip running/tasks */ - ++p; + SKIP_TOKEN(p); /* skip running/tasks */ /* last_pid */ /* diff --git a/pg_memusage.c b/pg_memusage.c index db2c941..e6a804f 100644 --- a/pg_memusage.c +++ b/pg_memusage.c @@ -15,20 +15,6 @@ #include #include "pg_common.h" -#ifdef __linux__ -static inline char *skip_token(const char *); - -static inline char * -skip_token(const char *p) -{ - while (isspace(*p)) - p++; - while (*p && !isspace(*p)) - p++; - return (char *) p; -} -#endif /* __linux__ */ - enum loadavg {i_memused, i_memfree, i_memshared, i_membuffers, i_memcached, i_swapused, i_swapfree, i_swapcached}; @@ -167,67 +153,51 @@ get_memusage(char **values) ++p; if (strncmp(p, "Buffers:", 8) == 0) { - p = skip_token(p); - - while (p[0] == ' ') - ++p; + SKIP_TOKEN(p); GET_NEXT_VALUE(p, q, values[i_membuffers], length, "Buffers not found", ' '); } else if (strncmp(p, "Cached:", 7) == 0) { - p = skip_token(p); - - while (p[0] == ' ') - ++p; + SKIP_TOKEN(p); GET_NEXT_VALUE(p, q, values[i_memcached], length, "Cached not found", ' '); } else if (strncmp(p, "MemFree:", 8) == 0) { - p = skip_token(p); - + SKIP_TOKEN(p); memfree = strtoul(p, &p, 10); sprintf(values[i_memused], "%lu", memtotal - memfree); sprintf(values[i_memfree], "%lu", memfree); } else if (strncmp(p, "MemShared:", 10) == 0) { - p = skip_token(p); - - while (p[0] == ' ') - ++p; + SKIP_TOKEN(p); GET_NEXT_VALUE(p, q, values[i_memshared], length, "MemShared not found", ' '); } else if (strncmp(p, "MemTotal:", 9) == 0) { - p = skip_token(p); - + SKIP_TOKEN(p); memtotal = strtoul(p, &p, 10); elog(DEBUG5, "pg_memusage: MemTotal = %lu", memtotal); } else if (strncmp(p, "SwapFree:", 9) == 0) { - p = skip_token(p); - + SKIP_TOKEN(p); swapfree = strtoul(p, &p, 10); sprintf(values[i_swapused], "%lu", swaptotal - swapfree); sprintf(values[i_swapfree], "%lu", swapfree); } else if (strncmp(p, "SwapCached:", 11) == 0) { - p = skip_token(p); - - while (p[0] == ' ') - ++p; + SKIP_TOKEN(p); GET_NEXT_VALUE(p, q, values[i_swapcached], length, "SwapCached not found", ' '); } else if (strncmp(p, "SwapTotal:", 10) == 0) { - p = skip_token(p); - + SKIP_TOKEN(p); swaptotal = strtoul(p, &p, 10); elog(DEBUG5, "pg_memusage: SwapTotal = %lu", swaptotal); } diff --git a/pg_proctab.c b/pg_proctab.c index b68b404..634a35c 100644 --- a/pg_proctab.c +++ b/pg_proctab.c @@ -14,28 +14,6 @@ #include #include "pg_common.h" -#ifdef __linux__ -#include - -/* - * For details on the Linux process table, see the description of - * /proc/PID/stat in Documentation/filesystems/proc.txt in the Linux source - * code. - */ - -static inline char *skip_token(const char *); - -static inline char * -skip_token(const char *p) -{ - while (isspace(*p)) - p++; - while (*p && !isspace(*p)) - p++; - return (char *) p; -} -#endif /* __linux__ */ - #define FULLCOMM_LEN 1024 #define GET_PIDS \ @@ -213,6 +191,12 @@ int get_proctab(FuncCallContext *funcctx, char **values) { #ifdef __linux__ + /* + * For details on the Linux process table, see the description of + * /proc/PID/stat in Documentation/filesystems/proc.txt in the Linux source + * code. + */ + int32 *ppid; int32 pid; int length; @@ -388,20 +372,19 @@ get_proctab(FuncCallContext *funcctx, char **values) /* rss */ GET_NEXT_VALUE(p, q, values[i_rss], length, "rss not found", ' '); - p = skip_token(p); /* skip rlim */ - p = skip_token(p); /* skip startcode */ - p = skip_token(p); /* skip endcode */ - p = skip_token(p); /* skip startstack */ - p = skip_token(p); /* skip kstkesp */ - p = skip_token(p); /* skip kstkeip */ - p = skip_token(p); /* skip signal (obsolete) */ - p = skip_token(p); /* skip blocked (obsolete) */ - p = skip_token(p); /* skip sigignore (obsolete) */ - p = skip_token(p); /* skip sigcatch (obsolete) */ - p = skip_token(p); /* skip wchan */ - p = skip_token(p); /* skip nswap (place holder) */ - p = skip_token(p); /* skip cnswap (place holder) */ - ++p; + SKIP_TOKEN(p); /* skip rlim */ + SKIP_TOKEN(p); /* skip startcode */ + SKIP_TOKEN(p); /* skip endcode */ + SKIP_TOKEN(p); /* skip startstack */ + SKIP_TOKEN(p); /* skip kstkesp */ + SKIP_TOKEN(p); /* skip kstkeip */ + SKIP_TOKEN(p); /* skip signal (obsolete) */ + SKIP_TOKEN(p); /* skip blocked (obsolete) */ + SKIP_TOKEN(p); /* skip sigignore (obsolete) */ + SKIP_TOKEN(p); /* skip sigcatch (obsolete) */ + SKIP_TOKEN(p); /* skip wchan */ + SKIP_TOKEN(p); /* skip nswap (place holder) */ + SKIP_TOKEN(p); /* skip cnswap (place holder) */ /* exit_signal */ GET_NEXT_VALUE(p, q, values[i_exit_signal], length, -- 2.39.5