From 47dbb9ae1ffba949ebb78679b28503ebc36d8b2b Mon Sep 17 00:00:00 2001 From: Andrea Scian Date: Fri, 3 Jun 2016 11:19:32 +0200 Subject: [PATCH 1/3] Strip trailing whitespaces --- src/cpulimit.c | 18 +++++++++--------- src/list.c | 2 +- src/list.h | 2 +- src/process_group.c | 2 +- src/process_iterator.c | 2 +- src/process_iterator_apple.c | 4 ++-- src/process_iterator_freebsd.c | 2 +- src/process_iterator_linux.c | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/cpulimit.c b/src/cpulimit.c index 50eabea..04ebd0f 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -139,7 +139,7 @@ static void increase_priority() { int old_priority = getpriority(PRIO_PROCESS, 0); int priority = old_priority; while (setpriority(PRIO_PROCESS, 0, priority-1) == 0 && priority>MAX_PRIORITY) { - priority--; + priority--; } if (priority != old_priority) { if (verbose) printf("Priority changed to %d\n", priority); @@ -200,7 +200,7 @@ void limit_process(pid_t pid, double limit, int include_children) memset(&twork, 0, sizeof(struct timespec)); memset(&tsleep, 0, sizeof(struct timespec)); memset(&startwork, 0, sizeof(struct timeval)); - memset(&endwork, 0, sizeof(struct timeval)); + memset(&endwork, 0, sizeof(struct timeval)); //last working time in microseconds unsigned long workingtime = 0; //generic list item @@ -210,7 +210,7 @@ void limit_process(pid_t pid, double limit, int include_children) //get a better priority increase_priority(); - + //build the family init_process_group(&pgroup, pid, include_children); @@ -226,7 +226,7 @@ void limit_process(pid_t pid, double limit, int include_children) if (verbose) printf("No more processes.\n"); break; } - + //total cpu actual usage (range 0-1) //1 means that the processes are using 100% cpu double pcpu = -1; @@ -283,7 +283,7 @@ void limit_process(pid_t pid, double limit, int include_children) nanosleep(&twork, NULL); gettimeofday(&endwork, NULL); workingtime = timediff(&endwork, &startwork); - + long delay = workingtime - twork.tv_nsec/1000; if (c>0 && delay>10000) { //delay is too much! signal to user? @@ -413,7 +413,7 @@ int main(int argc, char **argv) { print_usage(stderr, 1); exit(1); } - + if (exe_ok + pid_ok + command_mode > 1) { fprintf(stderr,"Error: You must specify exactly one target process, either by name, pid, or command line\n"); print_usage(stderr, 1); @@ -446,7 +446,7 @@ int main(int argc, char **argv) { } printf("'\n"); } - + int child = fork(); if (child < 0) { exit(EXIT_FAILURE); @@ -525,6 +525,6 @@ int main(int argc, char **argv) { if (lazy) break; sleep(2); }; - + exit(0); } diff --git a/src/list.c b/src/list.c index 2a78358..1e4bd79 100644 --- a/src/list.c +++ b/src/list.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License diff --git a/src/list.h b/src/list.h index 0b43a2b..7da3374 100644 --- a/src/list.h +++ b/src/list.h @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License diff --git a/src/process_group.c b/src/process_group.c index 06d73a6..bef3419 100644 --- a/src/process_group.c +++ b/src/process_group.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License diff --git a/src/process_iterator.c b/src/process_iterator.c index 8b4019d..8199252 100644 --- a/src/process_iterator.c +++ b/src/process_iterator.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License diff --git a/src/process_iterator_apple.c b/src/process_iterator_apple.c index b878ed8..594963b 100644 --- a/src/process_iterator_apple.c +++ b/src/process_iterator_apple.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * + * * Author: Simon Sigurdhsson * */ diff --git a/src/process_iterator_freebsd.c b/src/process_iterator_freebsd.c index a638112..62e143c 100644 --- a/src/process_iterator_freebsd.c +++ b/src/process_iterator_freebsd.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License diff --git a/src/process_iterator_linux.c b/src/process_iterator_linux.c index c8cdd07..cd75212 100644 --- a/src/process_iterator_linux.c +++ b/src/process_iterator_linux.c @@ -2,7 +2,7 @@ * * cpulimit - a CPU limiter for Linux * - * Copyright (C) 2005-2012, by: Angelo Marletta + * Copyright (C) 2005-2012, by: Angelo Marletta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License From 1db66c558d0655e3e39fb4ab331c56c0775c767a Mon Sep 17 00:00:00 2001 From: Andrea Scian Date: Fri, 3 Jun 2016 11:19:17 +0200 Subject: [PATCH 2/3] Make preprocessor macro more portable The previous fails to compile with some gcc versions (e.g. the one from Android NKD r11c) --- src/cpulimit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpulimit.c b/src/cpulimit.c index 04ebd0f..0e064b2 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -43,7 +43,7 @@ #include #include -#ifdef __APPLE__ || __FREEBSD__ +#if defined(__APPLE__) || defined(__FREEBSD__) #include #endif From 0972caa02015a82382b2e0c2f2161ae2836bb37e Mon Sep 17 00:00:00 2001 From: Andrea Scian Date: Fri, 3 Jun 2016 10:54:46 +0200 Subject: [PATCH 3/3] Add android support Please note that this requires android-22 API because bsd_signal symbol is defined only version 22 or above. The work was done by Emiliano Firmino (elmiliox@gmail.com) in its project (https://github.com/Miliox/android-cpulimit), I just rework it a little bit --- Android.mk | 22 ++++++++++++++++++++++ README.md | 5 +++++ src/cpulimit.c | 4 ++++ 3 files changed, 31 insertions(+) create mode 100644 Android.mk diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..6f3f6fb --- /dev/null +++ b/Android.mk @@ -0,0 +1,22 @@ +# +# Android.mk +# Emiliano Firmino, 2015-11-17 19:28 +# minor changes by Andrea Scian +# + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := cpulimit + +LOCAL_SRC_FILES := src/cpulimit.c \ + src/list.c \ + src/memrchr.c \ + src/process_iterator.c \ + src/process_group.c + +include $(BUILD_EXECUTABLE) + +# vim:ft=make +# diff --git a/README.md b/README.md index 019579d..19a9023 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ On FreeBSD: $ gmake # cp src/cpulimit /usr/bin +On Android (tested with NDK r11c, API >=22): + + $ ndk-build APP_BUILD_SCRIPT=$(pwd)/Android.mk NDK_PROJECT_PATH=$(pwd) APP_PLATFORM=android-22 + # adb push src/cpulimit /data/cpulimit + Run unit tests: $ ./tests/process_iterator_test diff --git a/src/cpulimit.c b/src/cpulimit.c index 0e064b2..4cea6a6 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -38,7 +38,11 @@ #include #include #include +#ifdef ANDROID +#include +#else #include +#endif #include #include #include