line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* This file is part of the Linux::SysInfo Perl module. |
2
|
|
|
|
|
|
|
* See http://search.cpan.org/dist/Linux-SysInfo/ |
3
|
|
|
|
|
|
|
* Vincent Pit - 2007 */ |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#include /* LINUX_VERSION_CODE, KERNEL_VERSION() */ |
6
|
|
|
|
|
|
|
#include /* , sysinfo(), SI_LOAD_SHIFT */ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT |
9
|
|
|
|
|
|
|
#include "EXTERN.h" |
10
|
|
|
|
|
|
|
#include "perl.h" |
11
|
|
|
|
|
|
|
#include "XSUB.h" |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#define __PACKAGE__ "Linux::SysInfo" |
14
|
|
|
|
|
|
|
#define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1) |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
/* --- Extended fields ----------------------------------------------------- */ |
17
|
|
|
|
|
|
|
|
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 |
20
|
|
|
|
|
|
|
#else |
21
|
|
|
|
|
|
|
# define LS_HAS_EXTENDED 0 |
22
|
|
|
|
|
|
|
#endif |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
/* --- Keys ---------------------------------------------------------------- */ |
25
|
|
|
|
|
|
|
|
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, \ |
34
|
|
|
|
|
|
|
(V), LS_HASH(K)) |
35
|
|
|
|
|
|
|
#else |
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)) |
40
|
|
|
|
|
|
|
# else |
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)) |
43
|
|
|
|
|
|
|
# endif |
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)) |
47
|
|
|
|
|
|
|
#endif |
48
|
|
|
|
|
|
|
|
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); |
60
|
|
|
|
|
|
|
#if LS_HAS_EXTENDED |
61
|
|
|
|
|
|
|
LS_KEY_DECLARE(totalhigh); |
62
|
|
|
|
|
|
|
LS_KEY_DECLARE(freehigh); |
63
|
|
|
|
|
|
|
LS_KEY_DECLARE(mem_unit); |
64
|
|
|
|
|
|
|
#endif /* LS_HAS_EXTENDED */ |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
/* --- XS ------------------------------------------------------------------ */ |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
MODULE = Linux::SysInfo PACKAGE = Linux::SysInfo |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
PROTOTYPES: ENABLE |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
BOOT: |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
HV *stash; |
75
|
4
|
|
|
|
|
|
stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, TRUE); |
76
|
4
|
|
|
|
|
|
newCONSTSUB(stash, "LS_HAS_EXTENDED", newSViv(LS_HAS_EXTENDED)); |
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(uptime); |
79
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(load1); |
80
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(load5); |
81
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(load15); |
82
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(totalram); |
83
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(freeram); |
84
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(sharedram); |
85
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(bufferram); |
86
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(totalswap); |
87
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(freeswap); |
88
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(procs); |
89
|
|
|
|
|
|
|
#if LS_HAS_EXTENDED |
90
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(totalhigh); |
91
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(freehigh); |
92
|
4
|
|
|
|
|
|
LS_KEY_DEFINE(mem_unit); |
93
|
|
|
|
|
|
|
#endif /* LS_HAS_EXTENDED */ |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
SV *sysinfo() |
97
|
|
|
|
|
|
|
PROTOTYPE: |
98
|
|
|
|
|
|
|
PREINIT: |
99
|
|
|
|
|
|
|
struct sysinfo si; |
100
|
|
|
|
|
|
|
NV l; |
101
|
|
|
|
|
|
|
HV *hv; |
102
|
|
|
|
|
|
|
CODE: |
103
|
10
|
50
|
|
|
|
|
if (sysinfo(&si) == -1) XSRETURN_UNDEF; |
104
|
|
|
|
|
|
|
|
105
|
10
|
|
|
|
|
|
hv = newHV(); |
106
|
|
|
|
|
|
|
|
107
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, uptime, newSViv(si.uptime)); |
108
|
|
|
|
|
|
|
|
109
|
10
|
|
|
|
|
|
l = ((NV) si.loads[0]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT))); |
110
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, load1, newSVnv(l)); |
111
|
10
|
|
|
|
|
|
l = ((NV) si.loads[1]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT))); |
112
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, load5, newSVnv(l)); |
113
|
10
|
|
|
|
|
|
l = ((NV) si.loads[2]) / ((NV) (((U32) 1) << ((U32) SI_LOAD_SHIFT))); |
114
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, load15, newSVnv(l)); |
115
|
|
|
|
|
|
|
|
116
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, totalram, newSVuv(si.totalram)); |
117
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, freeram, newSVuv(si.freeram)); |
118
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, sharedram, newSVuv(si.sharedram)); |
119
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, bufferram, newSVuv(si.bufferram)); |
120
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, totalswap, newSVuv(si.totalswap)); |
121
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, freeswap, newSVuv(si.freeswap)); |
122
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, procs, newSVuv(si.procs)); |
123
|
|
|
|
|
|
|
#if LS_HAS_EXTENDED |
124
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, totalhigh, newSVuv(si.totalhigh)); |
125
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, freehigh, newSVuv(si.freehigh)); |
126
|
10
|
|
|
|
|
|
LS_KEY_STORE(hv, mem_unit, newSVuv(si.mem_unit)); |
127
|
|
|
|
|
|
|
#endif /* LS_HAS_EXTENDED */ |
128
|
|
|
|
|
|
|
|
129
|
10
|
|
|
|
|
|
RETVAL = newRV_noinc((SV *) hv); |
130
|
|
|
|
|
|
|
OUTPUT: |
131
|
|
|
|
|
|
|
RETVAL |
132
|
|
|
|
|
|
|
|