1 /* This file is part of the Linux::SysInfo Perl module.
2 * See http://search.cpan.org/dist/Linux-SysInfo/
3 * Vincent Pit - 2007 */
5 #include <linux/version.h> /* LINUX_VERSION_CODE, KERNEL_VERSION() */
6 #include <sys/sysinfo.h> /* <struct sysinfo>, sysinfo(), SI_LOAD_SHIFT */
8 #define PERL_NO_GET_CONTEXT
13 #define __PACKAGE__ "Linux::SysInfo"
15 /* --- Extended fields ----------------------------------------------------- */
17 #if ((defined(__i386__) || defined(__x86_64__)) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 23))) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 48))
18 # define LS_HAS_EXTENDED 1
20 # define LS_HAS_EXTENDED 0
23 /* --- Keys ---------------------------------------------------------------- */
25 #define LS_KEY(K) (ls_key_##K##_sv)
26 #if PERL_REVISION <= 4 || (PERL_REVISION == 5 && PERL_VERSION <= 6)
27 /* newSVpvn_share doesn't exist in perl-5.6.x */
28 # define LS_HASH(K) (ls_key_##K##_hash)
29 # define LS_KEY_DECLARE(K) STATIC const char LS_KEY(K)[] = #K; \
30 STATIC U32 LS_HASH(K) = 0
31 # define LS_KEY_DEFINE(K) PERL_HASH(LS_HASH(K), LS_KEY(K), sizeof(#K)-1)
32 # define LS_KEY_STORE(H,K,V) hv_store((H), LS_KEY(K), sizeof(#K)-1, \
35 # if PERL_REVISION > 5 || (PERL_REVISION == 5 && (PERL_VERSION > 9 || (PERL_VERSION == 9 && PERL_SUBVERSION >= 3)))
36 /* From perl-5.9.3 (#24802), the key is only a SVt_PV and one can get the hash
37 * value with the SvSHARED_HASH() macro. */
38 # define LS_HASH(K) SvSHARED_HASH(LS_KEY(K))
40 /* Before, the key was a SVt_PVIV and the hash was stored in the UV field. */
41 # define LS_HASH(K) SvUVX(LS_KEY(K))
43 # define LS_KEY_DECLARE(K) STATIC SV *LS_KEY(K) = NULL
44 # define LS_KEY_DEFINE(K) LS_KEY(K) = newSVpvn_share(#K, sizeof(#K)-1, 0)
45 # define LS_KEY_STORE(H,K,V) hv_store_ent((H), LS_KEY(K), (V), LS_HASH(K))
48 LS_KEY_DECLARE(uptime);
49 LS_KEY_DECLARE(load1);
50 LS_KEY_DECLARE(load5);
51 LS_KEY_DECLARE(load15);
52 LS_KEY_DECLARE(totalram);
53 LS_KEY_DECLARE(freeram);
54 LS_KEY_DECLARE(sharedram);
55 LS_KEY_DECLARE(bufferram);
56 LS_KEY_DECLARE(totalswap);
57 LS_KEY_DECLARE(freeswap);
58 LS_KEY_DECLARE(procs);
60 LS_KEY_DECLARE(totalhigh);
61 LS_KEY_DECLARE(freehigh);
62 LS_KEY_DECLARE(mem_unit);
63 #endif /* LS_HAS_EXTENDED */
65 /* --- XS ------------------------------------------------------------------ */
67 MODULE = Linux::SysInfo PACKAGE = Linux::SysInfo
74 stash = gv_stashpv(__PACKAGE__, TRUE);
75 newCONSTSUB(stash, "LS_HAS_EXTENDED", newSViv(LS_HAS_EXTENDED));
77 LS_KEY_DEFINE(uptime);
80 LS_KEY_DEFINE(load15);
81 LS_KEY_DEFINE(totalram);
82 LS_KEY_DEFINE(freeram);
83 LS_KEY_DEFINE(sharedram);
84 LS_KEY_DEFINE(bufferram);
85 LS_KEY_DEFINE(totalswap);
86 LS_KEY_DEFINE(freeswap);
89 LS_KEY_DEFINE(totalhigh);
90 LS_KEY_DEFINE(freehigh);
91 LS_KEY_DEFINE(mem_unit);
92 #endif /* LS_HAS_EXTENDED */
101 if (sysinfo(&si) == -1) {
107 LS_KEY_STORE(h, uptime, newSViv(si.uptime));
109 l = ((NV) si.loads[0]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
110 LS_KEY_STORE(h, load1, newSVnv(l));
111 l = ((NV) si.loads[1]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
112 LS_KEY_STORE(h, load5, newSVnv(l));
113 l = ((NV) si.loads[2]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
114 LS_KEY_STORE(h, load15, newSVnv(l));
116 LS_KEY_STORE(h, totalram, newSVuv(si.totalram));
117 LS_KEY_STORE(h, freeram, newSVuv(si.freeram));
118 LS_KEY_STORE(h, sharedram, newSVuv(si.sharedram));
119 LS_KEY_STORE(h, bufferram, newSVuv(si.bufferram));
120 LS_KEY_STORE(h, totalswap, newSVuv(si.totalswap));
121 LS_KEY_STORE(h, freeswap, newSVuv(si.freeswap));
122 LS_KEY_STORE(h, procs, newSVuv(si.procs));
124 LS_KEY_STORE(h, totalhigh, newSVuv(si.totalhigh));
125 LS_KEY_STORE(h, freehigh, newSVuv(si.freehigh));
126 LS_KEY_STORE(h, mem_unit, newSVuv(si.mem_unit));
127 #endif /* LS_HAS_EXTENDED */
129 RETVAL = newRV_noinc((SV *) h);