Permalink
Browse files

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)
  • Loading branch information...
2 parents f1d6848 + c0f6c19 commit bdeafd87e5f9c5a849778e68cd3668a72c8add2f @k4rtik k4rtik committed Jan 1, 2016
Showing with 509 additions and 90 deletions.
  1. +16 −6 .gitignore
  2. +20 −0 Contributing.md
  3. +10 −3 Makefile
  4. +151 −0 bindings/python/pycpslib_build.py
  5. +17 −0 bindings/python/setup.py
  6. +29 −8 common.h
  7. +4 −0 driver.c
  8. +8 −8 pslib.h
  9. +117 −65 pslib_linux.c
  10. +51 −0 tests/conftest.py
  11. +2 −0 tests/requirements.txt
  12. +59 −0 tests/test_cpu.py
  13. +25 −0 tests/test_users.py
View
@@ -1,14 +1,24 @@
-a.out
+*.dylib
+*.eggs
+*.gcda
+*.gch
+*.gcno
+*.gcov
*.o
+*.pyc
*.so
-*.dylib
+*.swp
+.cache
+.ruby-version
+__pycache__
+a.out
+bindings/python/build/
+bindings/python/pycpslib.egg-info/
driver
-nul
notes.org
-.ruby-version
-*.swp
+nul
+scratch
tags
-*.gch
# For autotools build system
.deps/
View
@@ -24,7 +24,27 @@ that can be cut and pasted into actual programs that people
use. Running `driver` should give display all the information that
cpslib is able to gather about the system.
+# Add test cases
+The `bindings/python` directory has the `pycpslib_build.py` script
+which can be used to create `cffi` based bindings for the
+library.
+
+1. Change this script as necessary when your new function has
+ been added and then run `python setup.py develop` to install a dev
+ version of the bindings into your virtualenv
+1. Build a shared library using the instructions
+ [here](https://github.com/nibrahim/cpslib/issues/20) and then set
+ your `LD_LIBRARY_PATH` so that this can get picked up.
+1. Add a test to the `tests` directory in a new file or existing file
+ as appropriate.
+1. Install requirements to run tests. They're mentioned in
+ `tests/requirements.txt` so `pip install -r tests/requirements.txt`
+ should do it.
+1. Run the tests using `py.test tests` and they should all work fine.
+
+This process of running will be automated but it's that's not done yet.
+
# Commits
Please make sure that the the commits you make on every branch are
View
@@ -1,5 +1,5 @@
-CFLAGS = -fPIC -Wall -Werror -Wunused -Wextra -O2 -g -std=gnu11
-LDFLAGS = -dynamiclib
+CFLAGS = -fPIC -Wall -Werror -Wunused -Wextra -O2 -g -std=gnu11 -fprofile-arcs -ftest-coverage
+LDFLAGS = -dynamiclib -fprofile-arcs
RM = rm -rf
TARGET_LIB = libpslib.dylib
EXEC = driver
@@ -19,6 +19,13 @@ $(TARGET_LIB): $(OBJS)
$(EXEC): $(EXEC).c $(TARGET_LIB)
$(CC) -o $@ $< -L. -lpslib -Wl,-rpath .
+.PHONY: covclean
+covclean:
+ ${RM} *.gcno *.gcda *.gcov
+
.PHONY: clean
-clean:
+clean: covclean
${RM} ${TARGET_LIB} ${OBJS} $(EXEC) *dSYM
+
+check-syntax:
+ gcc -Wall -o /dev/null -S ${CHK_SOURCES}
@@ -0,0 +1,151 @@
+# pycpslib.py
+
+import os
+
+from cffi import FFI
+
+ffi = FFI()
+project_root = os.path.abspath("../../")
+
+ffi.set_source("pycpslib",
+ """#include <stdio.h>
+ #include <stdlib.h>
+ #include <sys/types.h>
+ #include <unistd.h>
+ #include "pslib.h"
+ """,
+ libraries = ["pslib"],
+ library_dirs = [project_root],
+ include_dirs = [project_root])
+
+
+ffi.cdef("""
+typedef struct {
+ char *device;
+ char *mountpoint;
+ char *fstype;
+ char *opts;
+} DiskPartition; /* TBD: Pluralise */
+
+typedef struct {
+ int nitems;
+ DiskPartition *partitions;
+} DiskPartitionInfo;
+
+typedef struct {
+ unsigned long total;
+ unsigned long used;
+ unsigned long free;
+ float percent;
+} DiskUsage;
+
+typedef struct {
+ char *name;
+ unsigned long readbytes;
+ unsigned long writebytes;
+ unsigned long reads;
+ unsigned long writes;
+ unsigned long readtime;
+ unsigned long writetime;
+} DiskIOCounters;
+
+typedef struct {
+ int nitems;
+ DiskIOCounters *iocounters;
+} DiskIOCounterInfo;
+
+typedef struct {
+ char * name;
+ unsigned long bytes_sent;
+ unsigned long bytes_recv;
+ unsigned long packets_sent;
+ unsigned long packets_recv;
+ unsigned long errin;
+ unsigned long errout;
+ unsigned long dropin;
+ unsigned long dropout;
+} NetIOCounters;
+
+typedef struct {
+ int nitems;
+ NetIOCounters *iocounters;
+} NetIOCounterInfo;
+
+typedef struct {
+ char *username;
+ char *tty;
+ char *hostname;
+ float tstamp;
+} Users;
+
+typedef struct {
+ int nitems;
+ Users *users;
+} UsersInfo;
+
+UsersInfo *get_users(void);
+
+long int get_boot_time(void);
+
+typedef struct {
+ unsigned long total;
+ unsigned long available;
+ float percent;
+ unsigned long used;
+ unsigned long free;
+ unsigned long active;
+ unsigned long inactive;
+ unsigned long buffers;
+ unsigned long cached;
+} VmemInfo;
+
+typedef struct {
+ unsigned long total;
+ unsigned long used;
+ unsigned long free;
+ float percent;
+ unsigned long sin;
+ unsigned long sout;
+} SwapMemInfo;
+
+typedef struct {
+ double user;
+ double system;
+ double idle;
+ double nice;
+ double iowait;
+ double irq;
+ double softirq;
+ double steal;
+ double guest;
+ double guest_nice;
+} CpuTimes;
+
+CpuTimes *cpu_times(int);
+int cpu_count(int);
+
+CpuTimes *cpu_times_percent(int, CpuTimes *);
+
+double *cpu_util_percent(int percpu, CpuTimes *prev_times);
+
+typedef struct {
+ int pid;
+ int ppid;
+ char *name;
+ char *exe;
+ char *cmdline;
+ double create_time;
+ unsigned int uid;
+ unsigned int euid;
+ unsigned int suid;
+ unsigned int gid;
+ unsigned int egid;
+ unsigned int sgid;
+ char *username;
+ char *terminal;
+} Process;
+
+""")
+
+if __name__ == '__main__':
+ ffi.compile()
@@ -0,0 +1,17 @@
+from setuptools import setup, find_packages
+
+setup(name = "pycpslib",
+ version = "0.1",
+ packages = find_packages(),
+ setup_requires=["cffi>=1.0.0"],
+ cffi_modules=["pycpslib_build.py:ffi"],
+ install_requires = ["cffi>=1.0.0"],
+ author = "Noufal Ibrahim",
+ author_email = "noufal@nibrahim.net.in",
+ description = "Python bindings for the cpslib library",
+ license = "MIT",
+ keywords = "tests",
+ url = "http://github.com/nibrahim/cpslib",
+ )
+
+
View
@@ -8,29 +8,50 @@
float percentage(unsigned long int, unsigned long int);
int str_comp(const void *, const void *);
int int_comp(const void *, const void *);
-//char *grep_awk(FILE *, const char *, int, const char *);
+char *grep_awk(FILE *, const char *, int, const char *);
char *squeeze(char *, const char *);
#ifdef NDEBUG
#define debug(M, ...)
#else
-#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#define debug(M, ...) \
+ fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
-#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, __LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)
+#define log_err(M, ...) \
+ fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %d, %s) " M "\n", __FILE__, \
+ __LINE__, __FUNCTION__, errno, clean_errno(), ##__VA_ARGS__)
-#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
+#define log_warn(M, ...) \
+ fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
+ clean_errno(), ##__VA_ARGS__)
-#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#define log_info(M, ...) \
+ fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
-#define check(A, M, ...) if (!(A)) {log_err(M, ##__VA_ARGS__); errno=0; goto error; }
+#define check(A, M, ...) \
+ if (!(A)) { \
+ log_err(M, ##__VA_ARGS__); \
+ errno = 0; \
+ goto error; \
+ }
-#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno = 0; goto error; }
+#define sentinel(M, ...) \
+ { \
+ log_err(M, ##__VA_ARGS__); \
+ errno = 0; \
+ goto error; \
+ }
#define check_mem(A) check((A), "Out of memory.")
-#define check_debug(A, M, ...) if (!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }
+#define check_debug(A, M, ...) \
+ if (!(A)) { \
+ debug(M, ##__VA_ARGS__); \
+ errno = 0; \
+ goto error; \
+ }
#endif
View
@@ -25,6 +25,10 @@ void test_getusers() {
void test_boottime() {
float t = get_boot_time();
printf(" -- boot_time \n");
+ if (t == -1) {
+ printf("Aborting\n");
+ return;
+ }
printf("%.1f\n\n", t);
}
View
@@ -168,12 +168,12 @@ typedef struct {
} CpuTimes;
typedef struct {
- unsigned int pid;
- unsigned int ppid;
+ pid_t pid;
+ pid_t ppid;
char *name;
char *exe;
char *cmdline;
- unsigned long create_time;
+ double create_time;
unsigned int uid;
unsigned int euid;
unsigned int suid;
@@ -189,16 +189,16 @@ int disk_usage(char[], DiskUsage *);
DiskPartitionInfo *disk_partitions(int);
void free_disk_partition_info(DiskPartitionInfo *);
-DiskIOCounterInfo *disk_io_counters();
+DiskIOCounterInfo *disk_io_counters(void);
void free_disk_iocounter_info(DiskIOCounterInfo *);
-NetIOCounterInfo *net_io_counters();
+NetIOCounterInfo *net_io_counters(void);
void free_net_iocounter_info(NetIOCounterInfo *);
-UsersInfo *get_users();
+UsersInfo *get_users(void);
void free_users_info(UsersInfo *);
-float get_boot_time();
+float get_boot_time(void);
int virtual_memory(VmemInfo *);
int swap_memory(SwapMemInfo *);
@@ -211,7 +211,7 @@ double *cpu_util_percent(int percpu, CpuTimes *prev_times);
int cpu_count(int);
-Process *get_process(unsigned int);
+Process *get_process(pid_t);
void free_process(Process *);
#endif
Oops, something went wrong.

0 comments on commit bdeafd8

Please sign in to comment.