Skip to content

Commit bdeafd8

Browse files
committed
Merge branch 'master' into osx
Merge Notes: - skipped merging gcov related changes, didn't work on osx. - skipped merging tests for unimplemented APIs - test infra exposed some Linux-specific in cpu_times(), need to get rid of them. Tests skip them (TODO: tests should be aware of platform in the future)
2 parents f1d6848 + c0f6c19 commit bdeafd8

13 files changed

+509
-90
lines changed

.gitignore

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
a.out
1+
*.dylib
2+
*.eggs
3+
*.gcda
4+
*.gch
5+
*.gcno
6+
*.gcov
27
*.o
8+
*.pyc
39
*.so
4-
*.dylib
10+
*.swp
11+
.cache
12+
.ruby-version
13+
__pycache__
14+
a.out
15+
bindings/python/build/
16+
bindings/python/pycpslib.egg-info/
517
driver
6-
nul
718
notes.org
8-
.ruby-version
9-
*.swp
19+
nul
20+
scratch
1021
tags
11-
*.gch
1222

1323
# For autotools build system
1424
.deps/

Contributing.md

+20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,27 @@ that can be cut and pasted into actual programs that people
2424
use. Running `driver` should give display all the information that
2525
cpslib is able to gather about the system.
2626

27+
# Add test cases
2728

29+
The `bindings/python` directory has the `pycpslib_build.py` script
30+
which can be used to create `cffi` based bindings for the
31+
library.
32+
33+
1. Change this script as necessary when your new function has
34+
been added and then run `python setup.py develop` to install a dev
35+
version of the bindings into your virtualenv
36+
1. Build a shared library using the instructions
37+
[here](https://github.com/nibrahim/cpslib/issues/20) and then set
38+
your `LD_LIBRARY_PATH` so that this can get picked up.
39+
1. Add a test to the `tests` directory in a new file or existing file
40+
as appropriate.
41+
1. Install requirements to run tests. They're mentioned in
42+
`tests/requirements.txt` so `pip install -r tests/requirements.txt`
43+
should do it.
44+
1. Run the tests using `py.test tests` and they should all work fine.
45+
46+
This process of running will be automated but it's that's not done yet.
47+
2848
# Commits
2949

3050
Please make sure that the the commits you make on every branch are

Makefile

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CFLAGS = -fPIC -Wall -Werror -Wunused -Wextra -O2 -g -std=gnu11
2-
LDFLAGS = -dynamiclib
1+
CFLAGS = -fPIC -Wall -Werror -Wunused -Wextra -O2 -g -std=gnu11 -fprofile-arcs -ftest-coverage
2+
LDFLAGS = -dynamiclib -fprofile-arcs
33
RM = rm -rf
44
TARGET_LIB = libpslib.dylib
55
EXEC = driver
@@ -19,6 +19,13 @@ $(TARGET_LIB): $(OBJS)
1919
$(EXEC): $(EXEC).c $(TARGET_LIB)
2020
$(CC) -o $@ $< -L. -lpslib -Wl,-rpath .
2121

22+
.PHONY: covclean
23+
covclean:
24+
${RM} *.gcno *.gcda *.gcov
25+
2226
.PHONY: clean
23-
clean:
27+
clean: covclean
2428
${RM} ${TARGET_LIB} ${OBJS} $(EXEC) *dSYM
29+
30+
check-syntax:
31+
gcc -Wall -o /dev/null -S ${CHK_SOURCES}

bindings/python/pycpslib_build.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# pycpslib.py
2+
3+
import os
4+
5+
from cffi import FFI
6+
7+
ffi = FFI()
8+
project_root = os.path.abspath("../../")
9+
10+
ffi.set_source("pycpslib",
11+
"""#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <sys/types.h>
14+
#include <unistd.h>
15+
#include "pslib.h"
16+
""",
17+
libraries = ["pslib"],
18+
library_dirs = [project_root],
19+
include_dirs = [project_root])
20+
21+
22+
ffi.cdef("""
23+
typedef struct {
24+
char *device;
25+
char *mountpoint;
26+
char *fstype;
27+
char *opts;
28+
} DiskPartition; /* TBD: Pluralise */
29+
30+
typedef struct {
31+
int nitems;
32+
DiskPartition *partitions;
33+
} DiskPartitionInfo;
34+
35+
typedef struct {
36+
unsigned long total;
37+
unsigned long used;
38+
unsigned long free;
39+
float percent;
40+
} DiskUsage;
41+
42+
typedef struct {
43+
char *name;
44+
unsigned long readbytes;
45+
unsigned long writebytes;
46+
unsigned long reads;
47+
unsigned long writes;
48+
unsigned long readtime;
49+
unsigned long writetime;
50+
} DiskIOCounters;
51+
52+
typedef struct {
53+
int nitems;
54+
DiskIOCounters *iocounters;
55+
} DiskIOCounterInfo;
56+
57+
typedef struct {
58+
char * name;
59+
unsigned long bytes_sent;
60+
unsigned long bytes_recv;
61+
unsigned long packets_sent;
62+
unsigned long packets_recv;
63+
unsigned long errin;
64+
unsigned long errout;
65+
unsigned long dropin;
66+
unsigned long dropout;
67+
} NetIOCounters;
68+
69+
typedef struct {
70+
int nitems;
71+
NetIOCounters *iocounters;
72+
} NetIOCounterInfo;
73+
74+
typedef struct {
75+
char *username;
76+
char *tty;
77+
char *hostname;
78+
float tstamp;
79+
} Users;
80+
81+
typedef struct {
82+
int nitems;
83+
Users *users;
84+
} UsersInfo;
85+
86+
UsersInfo *get_users(void);
87+
88+
long int get_boot_time(void);
89+
90+
typedef struct {
91+
unsigned long total;
92+
unsigned long available;
93+
float percent;
94+
unsigned long used;
95+
unsigned long free;
96+
unsigned long active;
97+
unsigned long inactive;
98+
unsigned long buffers;
99+
unsigned long cached;
100+
} VmemInfo;
101+
102+
typedef struct {
103+
unsigned long total;
104+
unsigned long used;
105+
unsigned long free;
106+
float percent;
107+
unsigned long sin;
108+
unsigned long sout;
109+
} SwapMemInfo;
110+
111+
typedef struct {
112+
double user;
113+
double system;
114+
double idle;
115+
double nice;
116+
double iowait;
117+
double irq;
118+
double softirq;
119+
double steal;
120+
double guest;
121+
double guest_nice;
122+
} CpuTimes;
123+
124+
CpuTimes *cpu_times(int);
125+
int cpu_count(int);
126+
127+
CpuTimes *cpu_times_percent(int, CpuTimes *);
128+
129+
double *cpu_util_percent(int percpu, CpuTimes *prev_times);
130+
131+
typedef struct {
132+
int pid;
133+
int ppid;
134+
char *name;
135+
char *exe;
136+
char *cmdline;
137+
double create_time;
138+
unsigned int uid;
139+
unsigned int euid;
140+
unsigned int suid;
141+
unsigned int gid;
142+
unsigned int egid;
143+
unsigned int sgid;
144+
char *username;
145+
char *terminal;
146+
} Process;
147+
148+
""")
149+
150+
if __name__ == '__main__':
151+
ffi.compile()

bindings/python/setup.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(name = "pycpslib",
4+
version = "0.1",
5+
packages = find_packages(),
6+
setup_requires=["cffi>=1.0.0"],
7+
cffi_modules=["pycpslib_build.py:ffi"],
8+
install_requires = ["cffi>=1.0.0"],
9+
author = "Noufal Ibrahim",
10+
author_email = "noufal@nibrahim.net.in",
11+
description = "Python bindings for the cpslib library",
12+
license = "MIT",
13+
keywords = "tests",
14+
url = "http://github.com/nibrahim/cpslib",
15+
)
16+
17+

common.h

+29-8
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,50 @@
88
float percentage(unsigned long int, unsigned long int);
99
int str_comp(const void *, const void *);
1010
int int_comp(const void *, const void *);
11-
//char *grep_awk(FILE *, const char *, int, const char *);
11+
char *grep_awk(FILE *, const char *, int, const char *);
1212
char *squeeze(char *, const char *);
1313

1414
#ifdef NDEBUG
1515
#define debug(M, ...)
1616
#else
17-
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
17+
#define debug(M, ...) \
18+
fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
1819
#endif
1920

2021
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
2122

22-
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, __LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)
23+
#define log_err(M, ...) \
24+
fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, \
25+
__LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)
2326

24-
#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
27+
#define log_warn(M, ...) \
28+
fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
29+
clean_errno(), ##__VA_ARGS__)
2530

26-
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
31+
#define log_info(M, ...) \
32+
fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
2733

28-
#define check(A, M, ...) if (!(A)) {log_err(M, ##__VA_ARGS__); errno=0; goto error; }
34+
#define check(A, M, ...) \
35+
if (!(A)) { \
36+
log_err(M, ##__VA_ARGS__); \
37+
errno = 0; \
38+
goto error; \
39+
}
2940

30-
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno = 0; goto error; }
41+
#define sentinel(M, ...) \
42+
{ \
43+
log_err(M, ##__VA_ARGS__); \
44+
errno = 0; \
45+
goto error; \
46+
}
3147

3248
#define check_mem(A) check((A), "Out of memory.")
3349

34-
#define check_debug(A, M, ...) if (!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }
50+
#define check_debug(A, M, ...) \
51+
if (!(A)) { \
52+
debug(M, ##__VA_ARGS__); \
53+
errno = 0; \
54+
goto error; \
55+
}
3556

3657
#endif

driver.c

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void test_getusers() {
2525
void test_boottime() {
2626
float t = get_boot_time();
2727
printf(" -- boot_time \n");
28+
if (t == -1) {
29+
printf("Aborting\n");
30+
return;
31+
}
2832
printf("%.1f\n\n", t);
2933
}
3034

pslib.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ typedef struct {
168168
} CpuTimes;
169169

170170
typedef struct {
171-
unsigned int pid;
172-
unsigned int ppid;
171+
pid_t pid;
172+
pid_t ppid;
173173
char *name;
174174
char *exe;
175175
char *cmdline;
176-
unsigned long create_time;
176+
double create_time;
177177
unsigned int uid;
178178
unsigned int euid;
179179
unsigned int suid;
@@ -189,16 +189,16 @@ int disk_usage(char[], DiskUsage *);
189189
DiskPartitionInfo *disk_partitions(int);
190190
void free_disk_partition_info(DiskPartitionInfo *);
191191

192-
DiskIOCounterInfo *disk_io_counters();
192+
DiskIOCounterInfo *disk_io_counters(void);
193193
void free_disk_iocounter_info(DiskIOCounterInfo *);
194194

195-
NetIOCounterInfo *net_io_counters();
195+
NetIOCounterInfo *net_io_counters(void);
196196
void free_net_iocounter_info(NetIOCounterInfo *);
197197

198-
UsersInfo *get_users();
198+
UsersInfo *get_users(void);
199199
void free_users_info(UsersInfo *);
200200

201-
float get_boot_time();
201+
float get_boot_time(void);
202202

203203
int virtual_memory(VmemInfo *);
204204
int swap_memory(SwapMemInfo *);
@@ -211,7 +211,7 @@ double *cpu_util_percent(int percpu, CpuTimes *prev_times);
211211

212212
int cpu_count(int);
213213

214-
Process *get_process(unsigned int);
214+
Process *get_process(pid_t);
215215
void free_process(Process *);
216216

217217
#endif

0 commit comments

Comments
 (0)