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
3 changes: 0 additions & 3 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
CPULIMIT
========

About
-----

Cpulimit is a tool which limits the CPU usage of a process (expressed in percentage, not in CPU time). It is useful to control batch jobs, when you don't want them to eat too many CPU cycles. The goal is prevent a process from running for more than a specified time ratio. It does not change the nice value or other scheduling priority settings, but the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.
The control of the used CPU amount is done sending SIGSTOP and SIGCONT POSIX signals to processes.
All the children processes and threads of the specified process will share the same percentage of CPU.
Expand Down
8 changes: 7 additions & 1 deletion src/cpulimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <sys/types.h>
#include <sys/wait.h>

#ifdef __APPLE__ || __FREEBSD__
#include <libgen.h>
#endif

#include "process_group.h"
#include "list.h"

Expand Down Expand Up @@ -147,7 +151,7 @@ static void increase_priority() {

/* Get the number of CPUs */
static int get_ncpu() {
int ncpu = -1;
int ncpu;
#ifdef _SC_NPROCESSORS_ONLN
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined __APPLE__
Expand All @@ -156,6 +160,8 @@ static int get_ncpu() {
sysctl(mib, 2, &ncpu, &len, NULL, 0);
#elif defined _GNU_SOURCE
ncpu = get_nprocs();
#else
ncpu = -1;
#endif
return ncpu;
}
Expand Down
7 changes: 3 additions & 4 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void delete_node(struct list *l,struct list_node *node) {
}
l->count--;
free(node);
node = NULL;
}

void destroy_node(struct list *l,struct list_node *node) {
Expand All @@ -75,7 +74,7 @@ void destroy_node(struct list *l,struct list_node *node) {
delete_node(l,node);
}

int is_EMPTYLIST_list(struct list *l) {
int is_empty_list(struct list *l) {
return (l->count==0?TRUE:FALSE);
}

Expand Down Expand Up @@ -123,8 +122,8 @@ void *locate_elem(struct list *l,void *elem) {
}

void clear_list(struct list *l) {
struct list_node *tmp;
while(l->first!=EMPTYLIST) {
struct list_node *tmp;
tmp=l->first;
l->first=l->first->next;
free(tmp);
Expand All @@ -135,8 +134,8 @@ void clear_list(struct list *l) {
}

void destroy_list(struct list *l) {
struct list_node *tmp;
while(l->first!=EMPTYLIST) {
struct list_node *tmp;
tmp=l->first;
l->first=l->first->next;
free(tmp->data);
Expand Down
5 changes: 2 additions & 3 deletions src/memrchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ memrchr(s, c, n)
int c;
size_t n;
{
const unsigned char *cp;

if (n != 0) {
cp = (unsigned char *)s + n;
const unsigned char *cp;
cp = (unsigned char *)s + n;
do {
if (*(--cp) == (unsigned char)c)
return((void *)cp);
Expand Down
1 change: 0 additions & 1 deletion src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ void update_process_group(struct process_group *pgroup)
else
{
assert(tmp_process.pid == p->pid);
assert(tmp_process.ppid == p->ppid);
assert(tmp_process.starttime == p->starttime);
add_elem(pgroup->proclist, p);
if (dt < MIN_DT) continue;
Expand Down
4 changes: 2 additions & 2 deletions src/process_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct process {
//ppid of the process
pid_t ppid;
//start time (unix timestamp)
int starttime;
time_t starttime;
//cputime used by the process (in milliseconds)
int cputime;
//actual cpu usage estimation (value in range 0-1)
Expand All @@ -74,7 +74,7 @@ struct process_filter {
struct process_iterator {
#ifdef __linux__
DIR *dip;
int boot_time;
time_t boot_time;
#elif defined __FreeBSD__
kvm_t *kd;
struct kinfo_proc *procs;
Expand Down
6 changes: 3 additions & 3 deletions src/process_iterator_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

#include <sys/vfs.h>

static int get_boot_time()
static time_t get_boot_time()
{
int uptime;
int uptime = 0;
FILE *fp = fopen ("/proc/uptime", "r");
if (fp != NULL)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ static int read_process_info(pid_t pid, struct process *p)
return -1;
}
fclose(fd);
sscanf(buffer, "%s", p->command);
strcpy(p->command, buffer);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/process_iterator_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <signal.h>
#include <string.h>

#ifdef __APPLE__
#ifdef __APPLE__ || __FREEBSD__
#include <libgen.h>
#endif

Expand Down