Contain Linux specific code within #ifdef __linux__.
authorMark Wong <markwkm@gmail.com>
Tue, 26 Aug 2008 05:25:59 +0000 (22:25 -0700)
committerMark Wong <markwkm@gmail.com>
Tue, 26 Aug 2008 05:25:59 +0000 (22:25 -0700)
pg_cputime.c
pg_loadavg.c
pg_memusage.c

index 45a6ec1af6b7632eb835b2422f0209dc08328af1..4daa6557a267ea0562aeb1000524dff5eb87b46d 100644 (file)
 #include <sys/param.h>
 #include <executor/spi.h>
 
+#ifdef PG_MODULE_MAGIC
+PG_MODULE_MAGIC;
+#endif
+
+#define BIGINT_LEN 20
+#define INTEGER_LEN 10
+
+#ifdef __linux__
 #if 0
 #include <linux/proc_fs.h>
 #else
 #define PROC_SUPER_MAGIC 0x9fa0
 #endif
 
-#ifdef PG_MODULE_MAGIC
-PG_MODULE_MAGIC;
-#endif
-
 #define PROCFS "/proc"
 
-#define BIGINT_LEN 20
-#define INTEGER_LEN 10
-
 #define GET_NEXT_VALUE(p, q, value, length, msg, delim) \
                if ((q = strchr(p, delim)) == NULL) \
                { \
@@ -41,9 +42,21 @@ PG_MODULE_MAGIC;
                value[length] = '\0'; \
                p = q + 1;
 
-Datum pg_cputime(PG_FUNCTION_ARGS);
 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__ */
+
+Datum pg_cputime(PG_FUNCTION_ARGS);
+
 PG_FUNCTION_INFO_V1(pg_cputime);
 
 Datum pg_cputime(PG_FUNCTION_ARGS)
@@ -54,12 +67,14 @@ Datum pg_cputime(PG_FUNCTION_ARGS)
        TupleDesc tupdesc;
        AttInMetadata *attinmeta;
 
+#ifdef __linux__
        struct statfs sb;
        int fd;
        int len;
        char buffer[4096];
        char *p;
        char *q;
+#endif /* __linux__ */
 
        enum loadavg {i_user, i_nice, i_system, i_idle, i_iowait};
 
@@ -117,6 +132,14 @@ Datum pg_cputime(PG_FUNCTION_ARGS)
 
                char **values = NULL;
 
+               values = (char **) palloc(5 * sizeof(char *));
+               values[i_user] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_nice] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_system] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_idle] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_iowait] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+
+#ifdef __linux__
                /*
                 * Sanity check, make sure we read the pid information that we're
                 * asking for.
@@ -133,13 +156,6 @@ Datum pg_cputime(PG_FUNCTION_ARGS)
                buffer[len] = '\0';
                elog(DEBUG5, "pg_cputime: %s", buffer);
 
-               values = (char **) palloc(5 * sizeof(char *));
-               values[i_user] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_nice] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_system] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_idle] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_iowait] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-
                p = buffer;
 
                p = skip_token(p);                      /* skip cpu */
@@ -167,6 +183,7 @@ Datum pg_cputime(PG_FUNCTION_ARGS)
                GET_NEXT_VALUE(p, q, values[i_iowait], length, "iowait not found",
                                ' ');
                elog(DEBUG5, "pg_cputime: iowait = %s", values[i_iowait]);
+#endif /* __linux__ */
 
                /* build a tuple */
                tuple = BuildTupleFromCStrings(attinmeta, values);
@@ -181,13 +198,3 @@ Datum pg_cputime(PG_FUNCTION_ARGS)
                SRF_RETURN_DONE(funcctx);
        }
 }
-
-static inline char *
-skip_token(const char *p)
-{
-       while (isspace(*p))
-               p++;
-       while (*p && !isspace(*p))
-               p++;
-       return (char *) p;
-}
index ec5d4c212c778b84bf332689da2f3fa2c94c9ec5..a17c9ad023f2414b1f081af311b3a588c1b22c54 100644 (file)
 #include <sys/param.h>
 #include <executor/spi.h>
 
+#ifdef __linux__
 #if 0
 #include <linux/proc_fs.h>
 #else
 #define PROC_SUPER_MAGIC 0x9fa0
 #endif
 
-#ifdef PG_MODULE_MAGIC
-PG_MODULE_MAGIC;
-#endif
-
 #define PROCFS "/proc"
 
-#define FLOAT_LEN 20
-#define INTEGER_LEN 10
-
 #define GET_NEXT_VALUE(p, q, value, length, msg, delim) \
                if ((q = strchr(p, delim)) == NULL) \
                { \
@@ -41,9 +35,28 @@ PG_MODULE_MAGIC;
                value[length] = '\0'; \
                p = q + 1;
 
-Datum pg_loadavg(PG_FUNCTION_ARGS);
 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__ */
+
+#ifdef PG_MODULE_MAGIC
+PG_MODULE_MAGIC;
+#endif
+
+#define FLOAT_LEN 20
+#define INTEGER_LEN 10
+
+Datum pg_loadavg(PG_FUNCTION_ARGS);
+
 PG_FUNCTION_INFO_V1(pg_loadavg);
 
 Datum pg_loadavg(PG_FUNCTION_ARGS)
@@ -54,12 +67,14 @@ Datum pg_loadavg(PG_FUNCTION_ARGS)
        TupleDesc tupdesc;
        AttInMetadata *attinmeta;
 
+#ifdef __linux__
        struct statfs sb;
        int fd;
        int len;
        char buffer[4096];
        char *p;
        char *q;
+#endif /* __linux__ */
 
        enum loadavg {i_load1, i_load5, i_load15, i_last_pid};
 
@@ -117,6 +132,13 @@ Datum pg_loadavg(PG_FUNCTION_ARGS)
 
                char **values = NULL;
 
+               values = (char **) palloc(4 * sizeof(char *));
+               values[i_load1] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
+               values[i_load5] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
+               values[i_load15] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
+               values[i_last_pid] = (char *) palloc((INTEGER_LEN + 1) * sizeof(char));
+
+#ifdef __linux__
                /*
                 * Sanity check, make sure we read the pid information that we're
                 * asking for.
@@ -133,12 +155,6 @@ Datum pg_loadavg(PG_FUNCTION_ARGS)
                buffer[len] = '\0';
                elog(DEBUG5, "pg_loadavg: %s", buffer);
 
-               values = (char **) palloc(4 * sizeof(char *));
-               values[i_load1] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
-               values[i_load5] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
-               values[i_load15] = (char *) palloc((FLOAT_LEN + 1) * sizeof(char));
-               values[i_last_pid] = (char *) palloc((INTEGER_LEN + 1) * sizeof(char));
-
                p = buffer;
 
                /* load1 */
@@ -176,6 +192,7 @@ Datum pg_loadavg(PG_FUNCTION_ARGS)
                }
                elog(DEBUG5, "pg_loadavg: last_pid = %s",
                                values[i_last_pid]);
+#endif /* __linux__ */
 
                /* build a tuple */
                tuple = BuildTupleFromCStrings(attinmeta, values);
@@ -190,13 +207,3 @@ Datum pg_loadavg(PG_FUNCTION_ARGS)
                SRF_RETURN_DONE(funcctx);
        }
 }
-
-static inline char *
-skip_token(const char *p)
-{
-       while (isspace(*p))
-               p++;
-       while (*p && !isspace(*p))
-               p++;
-       return (char *) p;
-}
index ecde4e342e4bdb59b23eaf3204b9bfe850670285..559cfd6bac57deb8cddbf9eb2fa4a4f11f75bfe5 100644 (file)
 #include <sys/param.h>
 #include <executor/spi.h>
 
-#if 0
-#include <linux/proc_fs.h>
-#else
-#define PROC_SUPER_MAGIC 0x9fa0
-#endif
-
 #ifdef PG_MODULE_MAGIC
 PG_MODULE_MAGIC;
 #endif
 
-#define PROCFS "/proc"
-
 #define BIGINT_LEN 20
 #define INTEGER_LEN 10
 
+#ifdef __linux__
+#define PROCFS "/proc"
+
+#if 0
+#include <linux/proc_fs.h>
+#else
+#define PROC_SUPER_MAGIC 0x9fa0
+#endif
+
 #define GET_NEXT_VALUE(p, q, value, length, msg, delim) \
                if ((q = strchr(p, delim)) == NULL) \
                { \
@@ -41,9 +42,22 @@ PG_MODULE_MAGIC;
                value[length] = '\0'; \
                p = q + 1;
 
-Datum pg_memusage(PG_FUNCTION_ARGS);
 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__ */
+
+Datum pg_memusage(PG_FUNCTION_ARGS);
+
 PG_FUNCTION_INFO_V1(pg_memusage);
 
 Datum pg_memusage(PG_FUNCTION_ARGS)
@@ -54,12 +68,14 @@ Datum pg_memusage(PG_FUNCTION_ARGS)
        TupleDesc tupdesc;
        AttInMetadata *attinmeta;
 
+#ifdef __linux__
        struct statfs sb;
        int fd;
        int len;
        char buffer[4096];
        char *p;
        char *q;
+#endif /* __linux__ */
 
        enum loadavg {i_memused, i_memfree, i_memshared, i_membuffers,
                        i_memcached, i_swapused, i_swapfree, i_swapcached};
@@ -122,6 +138,19 @@ Datum pg_memusage(PG_FUNCTION_ARGS)
 
                char **values = NULL;
 
+               values = (char **) palloc(8 * sizeof(char *));
+               values[i_memused] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_memfree] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_memshared] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_membuffers] =
+                               (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_memcached] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_swapused] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_swapfree] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+               values[i_swapcached] =
+                               (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
+
+#ifdef __linux__
                /*
                 * Sanity check, make sure we read the pid information that we're
                 * asking for.
@@ -138,18 +167,6 @@ Datum pg_memusage(PG_FUNCTION_ARGS)
                buffer[len] = '\0';
                elog(DEBUG5, "pg_memusage: %s", buffer);
 
-               values = (char **) palloc(8 * sizeof(char *));
-               values[i_memused] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_memfree] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_memshared] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_membuffers] =
-                               (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_memcached] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_swapused] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_swapfree] = (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-               values[i_swapcached] =
-                               (char *) palloc((BIGINT_LEN + 1) * sizeof(char));
-
                p = buffer - 1;
 
                values[i_memshared][0] = '0';
@@ -235,6 +252,7 @@ Datum pg_memusage(PG_FUNCTION_ARGS)
                        }
                        p = strchr(p, '\n');
                }
+#endif /* __linux__ */
 
                /* build a tuple */
                tuple = BuildTupleFromCStrings(attinmeta, values);
@@ -249,13 +267,3 @@ Datum pg_memusage(PG_FUNCTION_ARGS)
                SRF_RETURN_DONE(funcctx);
        }
 }
-
-static inline char *
-skip_token(const char *p)
-{
-       while (isspace(*p))
-               p++;
-       while (*p && !isspace(*p))
-               p++;
-       return (char *) p;
-}