Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android support #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions Android.mk
Original file line number Diff line number Diff line change
@@ -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
#
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/cpulimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -38,12 +38,16 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifdef ANDROID
#include <linux/sysctl.h>
#else
#include <sys/sysctl.h>
#endif
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>

#ifdef __APPLE__ || __FREEBSD__
#if defined(__APPLE__) || defined(__FREEBSD__)
#include <libgen.h>
#endif

Expand Down Expand Up @@ -139,7 +143,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);
Expand Down Expand Up @@ -200,7 +204,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
Expand All @@ -210,7 +214,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);

Expand All @@ -226,7 +230,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;
Expand Down Expand Up @@ -283,7 +287,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?
Expand Down Expand Up @@ -413,7 +417,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);
Expand Down Expand Up @@ -446,7 +450,7 @@ int main(int argc, char **argv) {
}
printf("'\n");
}

int child = fork();
if (child < 0) {
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -525,6 +529,6 @@ int main(int argc, char **argv) {
if (lazy) break;
sleep(2);
};

exit(0);
}
2 changes: 1 addition & 1 deletion src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
2 changes: 1 addition & 1 deletion src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
2 changes: 1 addition & 1 deletion src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
2 changes: 1 addition & 1 deletion src/process_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
4 changes: 2 additions & 2 deletions src/process_iterator_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -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
*
*/
Expand Down
2 changes: 1 addition & 1 deletion src/process_iterator_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down
2 changes: 1 addition & 1 deletion src/process_iterator_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* cpulimit - a CPU limiter for Linux
*
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down