Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions bindings/python/pycpslib_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
library_dirs = [project_root],
include_dirs = [project_root])

ffi.cdef('''
typedef int32_t pid_t;
typedef int32_t bool;
''')
ffi.cdef('''typedef int32_t pid_t;
typedef _Bool bool;''')

lines = open(project_root+"/pslib.h").readlines()

Expand Down
168 changes: 122 additions & 46 deletions driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ void test_diskusage() {
printf(" -- disk_usage \n");
disk_usage("/", &du);
printf("/\ntotal: %" PRIu64 "\nused: %" PRIu64 "\nfree: %" PRIu64
"\npercent: %f\n\n",
"\npercent: %.1f\n\n",
du.total, du.used, du.free, du.percent);
disk_usage("/etc", &du);
printf("/etc\ntotal: %" PRIu64 "\nused: %" PRIu64 "\nfree: %" PRIu64
"\npercent: %f\n\n",
"\npercent: %.1f\n\n",
du.total, du.used, du.free, du.percent);
}


void test_diskpartitioninfo() {
uint32_t i;
DiskPartitionInfo *phys_dp, *all_dp;
Expand Down Expand Up @@ -139,7 +140,7 @@ void test_virtualmeminfo() {
printf(" -- virtual_memory\n");
printf("Total: %" PRIu64 "\n", r.total);
printf("Available: %" PRIu64 "\n", r.available);
printf("Percent: %f\n", r.percent);
printf("Percent: %.1f\n", r.percent);
printf("Used: %" PRIu64 "\n", r.used);
printf("Free: %" PRIu64 "\n", r.free);
printf("Active: %" PRIu64 "\n", r.active);
Expand All @@ -160,7 +161,7 @@ void test_swap() {
printf("Total: %" PRIu64 "\n", r.total);
printf("Used: %" PRIu64 "\n", r.used);
printf("Free: %" PRIu64 "\n", r.free);
printf("Percent: %f\n", r.percent);
printf("Percent: %.1f\n", r.percent);
printf("Sin: %" PRIu64 "\n", r.sin);
printf("Sout: %" PRIu64 "\n", r.sout);
printf("\n");
Expand All @@ -174,16 +175,16 @@ void test_cpu_times() {
return;
}
printf(" -- cpu_times\n");
printf("User: %.3lf;", r->user);
printf(" Nice: %.3lf;", r->nice);
printf(" System: %.3lf;", r->system);
printf(" Idle: %.3lf;", r->idle);
printf(" IOWait: %.3lf;", r->iowait);
printf(" IRQ: %.3lf;", r->irq);
printf(" SoftIRQ: %.3lf;", r->softirq);
printf(" Steal: %.3lf;", r->steal);
printf(" Guest: %.3lf;", r->guest);
printf(" Guest nice: %.3lf\n", r->guest_nice);
printf("User: %.1lf;", r->user);
printf(" Nice: %.1lf;", r->nice);
printf(" System: %.1lf;", r->system);
printf(" Idle: %.1lf;", r->idle);
printf(" IOWait: %.1lf;", r->iowait);
printf(" IRQ: %.1lf;", r->irq);
printf(" SoftIRQ: %.1lf;", r->softirq);
printf(" Steal: %.1lf;", r->steal);
printf(" Guest: %.1lf;", r->guest);
printf(" Guest nice: %.1lf\n", r->guest_nice);
printf("\n\n");

free(r);
Expand All @@ -200,16 +201,16 @@ void test_cpu_times_percpu() {
printf(" -- cpu_times_percpu\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("CPU %" PRIu32 " :: ", i + 1);
printf(" Usr: %.3lf;", c->user);
printf(" Nice: %.3lf;", c->nice);
printf(" Sys: %.3lf;", c->system);
printf(" Idle: %.3lf;", c->idle);
printf(" IOWait: %.3lf;", c->iowait);
printf(" IRQ: %.3lf;", c->irq);
printf(" SoftIRQ: %.3lf;", c->softirq);
printf(" Steal: %.3lf;", c->steal);
printf(" Guest: %.3lf;", c->guest);
printf(" Guest nice: %.3lf\n", c->guest_nice);
printf(" Usr: %.1lf;", c->user);
printf(" Nice: %.1lf;", c->nice);
printf(" Sys: %.1lf;", c->system);
printf(" Idle: %.1lf;", c->idle);
printf(" IOWait: %.1lf;", c->iowait);
printf(" IRQ: %.1lf;", c->irq);
printf(" SoftIRQ: %.1lf;", c->softirq);
printf(" Steal: %.1lf;", c->steal);
printf(" Guest: %.1lf;", c->guest);
printf(" Guest nice: %.1lf\n", c->guest_nice);
printf("\n");
c++;
}
Expand All @@ -230,7 +231,7 @@ void test_cpu_util_percent() {
usleep(100000);
utilisation = cpu_util_percent(false, info);
printf(" -- cpu_util_percent\n");
printf("%f\n", *utilisation);
printf("%.1f\n", *utilisation);
printf("\n");

free(utilisation);
Expand All @@ -252,7 +253,7 @@ void test_cpu_util_percent_percpu() {
percentages = cpu_util_percent(1, info);
printf(" -- cpu_util_percent_percpu\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("Cpu #%" PRIu32 " : %f\n", i, percentages[i]);
printf("Cpu #%" PRIu32 " : %.1f\n", i, percentages[i]);
}

printf("\n");
Expand All @@ -277,16 +278,16 @@ void test_cpu_times_percent() {
}
printf(" -- cpu_times_percent\n");
printf("CPU times as percentage of total (0.1 second sample)\n");
printf("Usr: %.3lf;", ret->user);
printf(" Nice: %.3lf;", ret->nice);
printf(" Sys: %.3lf;", ret->system);
printf(" Idle: %.3lf;", ret->idle);
printf(" IOWait: %.3lf;", ret->iowait);
printf(" IRQ: %.3lf;", ret->irq);
printf(" SoftIRQ: %.3lf;", ret->softirq);
printf(" Steal: %.3lf;", ret->steal);
printf(" Guest: %.3lf;", ret->guest);
printf(" Guest nice: %.3lf\n", ret->guest_nice);
printf("Usr: %.1lf;", ret->user);
printf(" Nice: %.1lf;", ret->nice);
printf(" Sys: %.1lf;", ret->system);
printf(" Idle: %.1lf;", ret->idle);
printf(" IOWait: %.1lf;", ret->iowait);
printf(" IRQ: %.1lf;", ret->irq);
printf(" SoftIRQ: %.1lf;", ret->softirq);
printf(" Steal: %.1lf;", ret->steal);
printf(" Guest: %.1lf;", ret->guest);
printf(" Guest nice: %.1lf\n", ret->guest_nice);
printf("\n");
free(info);
free(ret);
Expand All @@ -312,16 +313,16 @@ void test_cpu_times_percent_percpu() {
printf("CPU times as percentage of total per CPU (0.1 second sample)\n");
for (uint32_t i = 0; i < ncpus; i++) {
printf("CPU %" PRIu32 " :: ", i + 1);
printf("Usr: %.3lf;", info->user);
printf(" Nice: %.3lf;", info->nice);
printf(" Sys: %.3lf;", info->system);
printf(" Idle: %.3lf;", info->idle);
printf(" IOWait: %.3lf;", info->iowait);
printf(" IRQ: %.3lf;", info->irq);
printf(" SoftIRQ: %.3lf;", info->softirq);
printf(" Steal: %.3lf;", info->steal);
printf(" Guest: %.3lf;", info->guest);
printf(" Guest nice: %.3lf\n", info->guest_nice);
printf("Usr: %.1lf;", info->user);
printf(" Nice: %.1lf;", info->nice);
printf(" Sys: %.1lf;", info->system);
printf(" Idle: %.1lf;", info->idle);
printf(" IOWait: %.1lf;", info->iowait);
printf(" IRQ: %.1lf;", info->irq);
printf(" SoftIRQ: %.1lf;", info->softirq);
printf(" Steal: %.1lf;", info->steal);
printf(" Guest: %.1lf;", info->guest);
printf(" Guest nice: %.1lf\n", info->guest_nice);
info++;
}

Expand Down Expand Up @@ -376,6 +377,79 @@ void test_process() {
free_process(process);
}

void test_net_connections()
{
char *status[] ={"ESTABLISHED","SYN_SENT","SYN_RECV","FIN_WAIT1","FIN_WAIT2","TIME_WAIT",
"CLOSE",
"CLOSE_WAIT",
"LAST_ACK",
"LISTEN",
"CLOSING",
"NONE",
"DELETE_TCB",
"IDLE",
"BOUND"
};
printf("\nnet_connections\n");
ConnInfo *result;
char *array[] ={"all", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "unix", "inet", "inet4", "inet6"};
uint32_t i=0,j=0;
for(j=0;j < 11;j++)
{
i=0;
result = net_connections(array[j]);
printf("\n%s\n",array[j]);
if(result)
{
while(i < result->nitems)
{
printf("sconn(fd=%d, family=%d, type=%d,",result->Connections[i].fd,result->Connections[i].family,result->Connections[i].type);
if(!(strcmp(result->Connections[i].laddr->ip, "NONE")))
printf(" laddr=(),");
else if((result->Connections[i].laddr->port == -1))
printf(" laddr='%s',",result->Connections[i].laddr->ip);
else
printf(" laddr=('%s', %d),",result->Connections[i].laddr->ip,result->Connections[i].laddr->port);
if(!(strcmp(result->Connections[i].raddr->ip, "NONE")))
printf(" raddr=(),");
else if(result->Connections[i].laddr->port == -1)
{
if(!strcmp(result->Connections[i].raddr->ip,""))
printf(" raddr=None,");
else
printf(" raddr='%s',",result->Connections[i].raddr->ip);
}
else
printf(" raddr=('%s', %d),",result->Connections[i].raddr->ip,result->Connections[i].raddr->port);
printf(" status='%s',", status[result->Connections[i].status]);
if(result->Connections[i].pid==-1)
printf(" pid=None)\n");
else
printf(" pid=%d)\n", result->Connections[i].pid);
i++;
}

}
free_ConnInfo(result);
printf("\n");
}
}
void test_netifstats()
{
NetIfStatsInfo *n;
NetIfStats *dp;
uint32_t i;
n = net_if_stats();
dp = n->ifstats;
printf(" -- net_if_stats (interface count: %" PRIu32 ")\n", n->nitems);
for (i = 0; i < n->nitems; i++) {
printf("%s\t:snicstats(isup=%s, duplex=%" PRIu32", speed=%" PRIu32", mtu=%" PRIu32")\n",dp->name,dp->isup?"true":"false",dp->duplex,dp->speed,dp->mtu);
dp++;
}
free_net_ifstats_info(n);
printf("\n");
}

int main(void) {
test_diskusage();
test_diskpartitioninfo();
Expand All @@ -398,4 +472,6 @@ int main(void) {
test_cpu_count();
test_pid_exists();
test_process();
test_net_connections();
test_netifstats();
}
70 changes: 70 additions & 0 deletions pslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include <sys/types.h>


enum proc_status {
STATUS_RUNNING,
STATUS_SLEEPING,
Expand Down Expand Up @@ -73,6 +74,26 @@ enum proc_priority {
REALTIME_PRIORITY_CLASS
};

typedef struct {
uint32_t duplex;
uint32_t speed;
}DuplexSpeed;

typedef struct {
char *name;
bool isup;
uint32_t duplex;
uint32_t speed;
uint32_t mtu;
}NetIfStats;


typedef struct {
uint32_t nitems;
NetIfStats *ifstats;
}NetIfStatsInfo;


typedef struct {
uint64_t total;
uint64_t used;
Expand Down Expand Up @@ -188,6 +209,46 @@ typedef struct {
char *terminal;
} Process;

//for storing list of pids
typedef struct {
pid_t *pid;
int size;
} Pidlist;
// structure to store ip address
typedef struct{
char ip[500];
int port;
}address ;
//stores details of connections
typedef struct{
int fd;
int family;
int type;
address *laddr;
address *raddr;
enum con_status status;
pid_t pid;
}Conn;

typedef struct{
uint32_t nitems;
Conn * Connections;
} ConnInfo;
//structure for storing famiy and type for each kind of connections
typedef struct{
char name[7];
int family;
int type_;
} Connection;
// structure for storing list of pid and fd for each inode
typedef struct i_nodes{
int inode;
struct i_nodes *Next_in_list;
struct i_nodes *Next_collision; /* to manage collision as hashing is implemented */
pid_t pid;
int fd;
} Inodes;

bool disk_usage(const char[], DiskUsage *);

DiskPartitionInfo *disk_partitions(bool);
Expand Down Expand Up @@ -220,8 +281,17 @@ bool pid_exists(pid_t);
Process *get_process(pid_t);
void free_process(Process *);

ConnInfo *net_connections(char kind[]);
void free_ConnInfo(ConnInfo *);

/* Required to avoid [-Wimplicit-function-declaration] for python bindings */
void gcov_flush(void);

NetIfStatsInfo *net_if_stats(void);
uint32_t net_if_mtu(char *);
DuplexSpeed * net_if_duplex_speed(char*);
uint32_t net_if_flags(char *);
void free_net_ifstats_info(NetIfStatsInfo *) ;
// disk_io_counters_per_disk
// net_io_counters_per_nic

Loading