aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbhackerozzo <bhackerozzo@3b445381-d045-0410-9223-e17ecdb95f4b>2008-08-31 17:26:06 +0000
committerbhackerozzo <bhackerozzo@3b445381-d045-0410-9223-e17ecdb95f4b>2008-08-31 17:26:06 +0000
commit229eaafc30e2bab864884702406ccc7ff5555674 (patch)
tree9caea85f88f155d7646ad289545f2d7e38fca2c6
parent176557951f2a7f0bf4ca0c24d6e7469494bddf0a (diff)
downloadcpulimit-229eaafc30e2bab864884702406ccc7ff5555674.tar.gz
__GNUC__ to __linux__
-rw-r--r--process.c8
-rw-r--r--process.h34
-rw-r--r--procutils.c17
-rw-r--r--procutils.h2
-rw-r--r--tools/ptest.c3
5 files changed, 42 insertions, 22 deletions
diff --git a/process.c b/process.c
index e570ad6..9b898ab 100644
--- a/process.c
+++ b/process.c
@@ -29,7 +29,7 @@
int process_init(struct process_history *proc, int pid)
{
-#ifdef __GNUC__
+#ifdef __linux__
//test /proc file descriptor for reading
sprintf(proc->stat_file, "/proc/%d/stat", pid);
FILE *fd = fopen(proc->stat_file, "r");
@@ -39,7 +39,7 @@ int process_init(struct process_history *proc, int pid)
//init properties
proc->pid = pid;
proc->cpu_usage = 0;
- memset(&(proc->last_sample), 0, sizeof(struct timespec));
+ memset(&(proc->last_sample), 0, sizeof(struct timeval));
proc->last_jiffies = -1;
return 0;
}
@@ -51,7 +51,7 @@ static inline unsigned long timediff(const struct timeval *t1,const struct timev
}
static int get_jiffies(struct process_history *proc) {
-#ifdef __GNUC__
+#ifdef __linux__
FILE *f = fopen(proc->stat_file, "r");
if (f==NULL) return -1;
fgets(proc->buffer, sizeof(proc->buffer),f);
@@ -104,7 +104,7 @@ int process_monitor(struct process_history *proc)
proc->cpu_usage = sample;
}
else {
- //adaptative adjustment
+ //usage adjustment
proc->cpu_usage = (1-ALFA) * proc->cpu_usage + ALFA * sample;
}
diff --git a/process.h b/process.h
index a9497fa..257c74d 100644
--- a/process.h
+++ b/process.h
@@ -33,24 +33,44 @@
#include <unistd.h>
//kernel time resolution timer interrupt frequency in Hertz
-#define HZ sysconf(_SC_CLK_TCK)
+//#define HZ sysconf(_SC_CLK_TCK)
+
+//HZ detection, from openssl code
+#ifndef HZ
+# if defined(_SC_CLK_TCK) \
+ && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
+# define HZ ((double)sysconf(_SC_CLK_TCK))
+# else
+# ifndef CLK_TCK
+# ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
+# define HZ 100.0
+# else /* _BSD_CLK_TCK_ */
+# define HZ ((double)_BSD_CLK_TCK_)
+# endif
+# else /* CLK_TCK */
+# define HZ ((double)CLK_TCK)
+# endif
+# endif
+#endif
+
//process descriptor
struct process_history {
//the PID of the process
pid_t pid;
-#ifdef __GNUC__
- //name of /proc/PID/stat file
- char stat_file[20];
- //read buffer for /proc filesystem
- char buffer[1024];
-#endif
//timestamp when last_j and cpu_usage was calculated
struct timeval last_sample;
//total number of jiffies used by the process at time last_sample
int last_jiffies;
//cpu usage estimation (value in range 0-1)
double cpu_usage;
+#ifdef __linux__
+ //preallocate buffers for performance
+ //name of /proc/PID/stat file
+ char stat_file[20];
+ //read buffer for /proc filesystem
+ char buffer[1024];
+#endif
};
int process_init(struct process_history *proc, pid_t pid);
diff --git a/procutils.c b/procutils.c
index d6dd438..6ad6517 100644
--- a/procutils.c
+++ b/procutils.c
@@ -27,7 +27,7 @@
// returns pid of the parent process
static pid_t getppid_of(pid_t pid)
{
-#ifdef __GNUC__
+#ifdef __linux__
char file[20];
char buffer[1024];
sprintf(file, "/proc/%d/stat", pid);
@@ -59,7 +59,7 @@ static pid_t getppid_of(pid_t pid)
// returns the start time of a process (used with pid to identify a process)
static int get_starttime(pid_t pid)
{
-#ifdef __GNUC__
+#ifdef __linux__
char file[20];
char buffer[1024];
sprintf(file, "/proc/%d/stat", pid);
@@ -89,7 +89,7 @@ static int get_starttime(pid_t pid)
// detects whether a process is a kernel thread or not
static int is_kernel_thread(pid_t pid)
{
-#ifdef __GNUC__
+#ifdef __linux__
static char statfile[20];
static char buffer[64];
int ret;
@@ -107,7 +107,7 @@ static int is_kernel_thread(pid_t pid)
// returns 1 if pid is a user process, 0 otherwise
static int process_exists(pid_t pid) {
-#ifdef __GNUC__
+#ifdef __linux__
static char statfile[20];
static char buffer[64];
int ret;
@@ -187,7 +187,7 @@ static int is_member(struct process_family *f, pid_t pid) {
// creates an object that browse all running processes
static int init_process_iterator(struct process_iterator *i) {
-#ifdef __GNUC__
+#ifdef __linux__
//open a directory stream to /proc directory
if ((i->dip = opendir("/proc")) == NULL) {
perror("opendir");
@@ -204,7 +204,8 @@ static int init_process_iterator(struct process_iterator *i) {
// automatic closing if the end of the list is reached
static int read_next_process(struct process_iterator *i) {
pid_t pid = 0;
-#ifdef __GNUC__
+#ifdef __linux__
+//TODO read this to port to other systems: http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC85
//read in from /proc and seek for process dirs
while ((i->dit = readdir(i->dip)) != NULL) {
pid = atoi(i->dit->d_name);
@@ -365,7 +366,7 @@ void cleanup_process_family(struct process_family *f)
int look_for_process_by_pid(pid_t pid)
{
if (process_exists(pid))
- return (kill(pid,SIGCONT)==0) ? pid : -pid;
+ return (kill(pid,0)==0) ? pid : -pid;
return 0;
}
@@ -398,7 +399,7 @@ int look_for_process_by_name(const char *process_name)
info.processName = (char*)malloc(64*sizeof(char));
#endif
while ((pid = read_next_process(&iter))) {
-#ifdef __GNUC__
+#ifdef __linux__
//read the executable link
sprintf(exelink,"/proc/%d/exe",pid);
int size = readlink(exelink, exepath, sizeof(exepath));
diff --git a/procutils.h b/procutils.h
index d0d2f3e..c285e15 100644
--- a/procutils.h
+++ b/procutils.h
@@ -72,7 +72,7 @@ struct process {
// object to enumerate running processes
struct process_iterator {
-#ifdef __GNUC__
+#ifdef __linux__
DIR *dip;
struct dirent *dit;
#elif defined __APPLE__
diff --git a/tools/ptest.c b/tools/ptest.c
index ee85a2a..68d1fc2 100644
--- a/tools/ptest.c
+++ b/tools/ptest.c
@@ -7,7 +7,7 @@
#include <stdlib.h>
#include <unistd.h>
-#define N 8
+#define N 10
//simple program to test cpulimit
int main()
@@ -15,7 +15,6 @@ int main()
printf("Parent: PID %d\n", getpid());
getchar();
sleep(1);
- while(1);
int i;
int children[N];
for (i=0;i<N;i++) {
Un proyecto texto-plano.xyz