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"
14 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
16 /* --- Extended fields ----------------------------------------------------- */
18 #if ((defined(__i386__) || defined(__x86_64__)) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 23))) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 48))
19 # define LS_HAS_EXTENDED 1
21 # define LS_HAS_EXTENDED 0
24 /* --- Keys ---------------------------------------------------------------- */
26 #define LS_KEY(K) (ls_key_##K##_sv)
27 #if PERL_REVISION <= 4 || (PERL_REVISION == 5 && PERL_VERSION <= 6)
28 /* newSVpvn_share doesn't exist in perl-5.6.x */
29 # define LS_HASH(K) (ls_key_##K##_hash)
30 # define LS_KEY_DECLARE(K) STATIC const char LS_KEY(K)[] = #K; \
31 STATIC U32 LS_HASH(K) = 0
32 # define LS_KEY_DEFINE(K) PERL_HASH(LS_HASH(K), LS_KEY(K), sizeof(#K)-1)
33 # define LS_KEY_STORE(H,K,V) hv_store((H), LS_KEY(K), sizeof(#K)-1, \
36 # if PERL_REVISION > 5 || (PERL_REVISION == 5 && (PERL_VERSION > 9 || (PERL_VERSION == 9 && PERL_SUBVERSION >= 3)))
37 /* From perl-5.9.3 (#24802), the key is only a SVt_PV and one can get the hash
38 * value with the SvSHARED_HASH() macro. */
39 # define LS_HASH(K) SvSHARED_HASH(LS_KEY(K))
41 /* Before, the key was a SVt_PVIV and the hash was stored in the UV field. */
42 # define LS_HASH(K) SvUVX(LS_KEY(K))
44 # define LS_KEY_DECLARE(K) STATIC SV *LS_KEY(K) = NULL
45 # define LS_KEY_DEFINE(K) LS_KEY(K) = newSVpvn_share(#K, sizeof(#K)-1, 0)
46 # define LS_KEY_STORE(H,K,V) hv_store_ent((H), LS_KEY(K), (V), LS_HASH(K))
49 LS_KEY_DECLARE(uptime);
50 LS_KEY_DECLARE(load1);
51 LS_KEY_DECLARE(load5);
52 LS_KEY_DECLARE(load15);
53 LS_KEY_DECLARE(totalram);
54 LS_KEY_DECLARE(freeram);
55 LS_KEY_DECLARE(sharedram);
56 LS_KEY_DECLARE(bufferram);
57 LS_KEY_DECLARE(totalswap);
58 LS_KEY_DECLARE(freeswap);
59 LS_KEY_DECLARE(procs);
61 LS_KEY_DECLARE(totalhigh);
62 LS_KEY_DECLARE(freehigh);
63 LS_KEY_DECLARE(mem_unit);
64 #endif /* LS_HAS_EXTENDED */
66 /* --- XS ------------------------------------------------------------------ */
68 MODULE = Linux::SysInfo PACKAGE = Linux::SysInfo
75 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, TRUE);
76 newCONSTSUB(stash, "LS_HAS_EXTENDED", newSViv(LS_HAS_EXTENDED));
78 LS_KEY_DEFINE(uptime);
81 LS_KEY_DEFINE(load15);
82 LS_KEY_DEFINE(totalram);
83 LS_KEY_DEFINE(freeram);
84 LS_KEY_DEFINE(sharedram);
85 LS_KEY_DEFINE(bufferram);
86 LS_KEY_DEFINE(totalswap);
87 LS_KEY_DEFINE(freeswap);
90 LS_KEY_DEFINE(totalhigh);
91 LS_KEY_DEFINE(freehigh);
92 LS_KEY_DEFINE(mem_unit);
93 #endif /* LS_HAS_EXTENDED */
102 if (sysinfo(&si) == -1)
107 LS_KEY_STORE(hv, uptime, newSViv(si.uptime));
109 l = ((NV) si.loads[0]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
110 LS_KEY_STORE(hv, load1, newSVnv(l));
111 l = ((NV) si.loads[1]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
112 LS_KEY_STORE(hv, load5, newSVnv(l));
113 l = ((NV) si.loads[2]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT)));
114 LS_KEY_STORE(hv, load15, newSVnv(l));
116 LS_KEY_STORE(hv, totalram, newSVuv(si.totalram));
117 LS_KEY_STORE(hv, freeram, newSVuv(si.freeram));
118 LS_KEY_STORE(hv, sharedram, newSVuv(si.sharedram));
119 LS_KEY_STORE(hv, bufferram, newSVuv(si.bufferram));
120 LS_KEY_STORE(hv, totalswap, newSVuv(si.totalswap));
121 LS_KEY_STORE(hv, freeswap, newSVuv(si.freeswap));
122 LS_KEY_STORE(hv, procs, newSVuv(si.procs));
124 LS_KEY_STORE(hv, totalhigh, newSVuv(si.totalhigh));
125 LS_KEY_STORE(hv, freehigh, newSVuv(si.freehigh));
126 LS_KEY_STORE(hv, mem_unit, newSVuv(si.mem_unit));
127 #endif /* LS_HAS_EXTENDED */
129 RETVAL = newRV_noinc((SV *) hv);