Implement getrusage

Implement getrusage.
These fields of struct rusage are not supported and always set to zero at this time
long ru_nswap;           /* swaps */
long ru_inblock;         /* block input operations */
long ru_oublock;         /* block output operations */
long ru_msgsnd;          /* messages sent */
long ru_msgrcv;          /* messages received */
long ru_nvcsw;           /* voluntary context switches */
long ru_nivcsw;          /* involuntary context switches */

test75.c is the unit test for this new function

Change-Id: I3f1eb69de1fce90d087d76773b09021fc6106539
This commit is contained in:
Xiaoguang Sun
2013-07-01 23:00:47 +02:00
committed by Gerrit Code Review
parent 4241cc5d98
commit 64f10ee644
41 changed files with 403 additions and 29 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ MINIX_TESTS= \
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
41 42 43 44 45 46 48 49 50 52 53 54 55 56 58 59 60 \
61 64 65 66 67 68 69 70 71 72 73 74
61 64 65 66 67 68 69 70 71 72 73 74 75
.if ${MACHINE_ARCH} == "i386"
MINIX_TESTS+= \
+1 -1
View File
@@ -24,7 +24,7 @@ setuids="test11 test33 test43 test44 test46 test56 test60 test61 test65 \
alltests=" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
61 62 63 64 65 66 67 68 69 70 71 72 \
61 62 63 64 65 66 67 68 69 70 71 72 75 \
sh1.sh sh2.sh interp.sh"
tests_no=`expr 0`
+114
View File
@@ -0,0 +1,114 @@
/* Test 75 - getrusage functionality test.
*/
#include <sys/resource.h>
#include <sys/time.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#define CHECK_ZERO_FIELD(rusage, field) \
if (rusage.field != 0) \
em(1, #field " must be zero");
#define CHECK_NOT_ZERO_FIELD(rusage, field) \
if (rusage.field == 0) \
em(1, #field " can't be zero");
#define CHECK_EQUAL_FIELD(rusage1, rusage2, field) \
if (rusage1.field != rusage2.field) \
em(1, #field " of " #rusage1 " doesn't equal to " \
#field " of " #rusage2);
static void spin()
{
struct timeval start_time;
struct timeval end_time;
int loop = 0;
if (gettimeofday(&start_time, NULL) == -1) {
e(1);
exit(1);
}
memset(&end_time, 0, sizeof(end_time));
while (start_time.tv_sec + 10 > end_time.tv_sec) {
if ((++loop % 10000) == 0) {
if (gettimeofday(&end_time, NULL) == -1) {
e(1);
exit(1);
}
}
}
}
int
main(int argc, char *argv[])
{
struct rusage r_usage1;
struct rusage r_usage2;
struct rusage r_usage3;
pid_t child;
int status = 0;
start(75);
if ((getrusage(RUSAGE_SELF + 1, &r_usage1) != -1 || errno != EINVAL) ||
(getrusage(RUSAGE_CHILDREN - 1, &r_usage1) != -1 ||
errno != EINVAL) || (getrusage(RUSAGE_SELF, NULL) != -1 ||
errno != EFAULT)) {
e(1);
exit(1);
}
spin();
if (getrusage(RUSAGE_SELF, &r_usage1) != 0) {
e(1);
exit(1);
}
CHECK_NOT_ZERO_FIELD(r_usage1, ru_utime.tv_sec);
CHECK_NOT_ZERO_FIELD(r_usage1, ru_maxrss);
CHECK_NOT_ZERO_FIELD(r_usage1, ru_ixrss);
CHECK_NOT_ZERO_FIELD(r_usage1, ru_idrss);
CHECK_NOT_ZERO_FIELD(r_usage1, ru_isrss);
if (getrusage(RUSAGE_CHILDREN, &r_usage2) != 0) {
e(1);
exit(1);
}
CHECK_ZERO_FIELD(r_usage2, ru_utime.tv_sec);
CHECK_ZERO_FIELD(r_usage2, ru_utime.tv_usec);
CHECK_NOT_ZERO_FIELD(r_usage2, ru_maxrss);
CHECK_NOT_ZERO_FIELD(r_usage2, ru_ixrss);
CHECK_NOT_ZERO_FIELD(r_usage2, ru_idrss);
CHECK_NOT_ZERO_FIELD(r_usage2, ru_isrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_ixrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_idrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_isrss);
if ((child = fork()) != 0) {
if (child != waitpid(child, &status, 0)) {
e(1);
exit(1);
}
if (WEXITSTATUS(status) != 0) {
e(1);
exit(1);
}
if (getrusage(RUSAGE_CHILDREN, &r_usage3) != 0) {
e(1);
exit(1);
}
CHECK_NOT_ZERO_FIELD(r_usage3, ru_utime.tv_sec);
CHECK_NOT_ZERO_FIELD(r_usage3, ru_maxrss);
CHECK_NOT_ZERO_FIELD(r_usage3, ru_ixrss);
CHECK_NOT_ZERO_FIELD(r_usage3, ru_idrss);
CHECK_NOT_ZERO_FIELD(r_usage3, ru_isrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_ixrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_idrss);
CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_isrss);
} else {
spin();
exit(0);
}
quit();
return 0;
}