"Fossies" - the Fresh Open Source Software Archive 
Member "memtime-1.3/memtime.c" (21 Jan 2002, 5555 Bytes) of package /linux/misc/old/memtime-1.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "memtime.c" see the
Fossies "Dox" file reference documentation.
1 /* -*- mode: C; c-file-style: "k&r"; -*-
2 *---------------------------------------------------------------------------*
3 *
4 * Copyright (c) 2000, Johan Bengtsson
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 *---------------------------------------------------------------------------*/
24
25 #define _USE_BSD
26
27 #include <sys/time.h>
28 #include <sys/resource.h>
29 #include <sys/wait.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <signal.h>
38
39 #include <errno.h>
40
41 #include "machdep.h"
42
43 #define CAN_USE_RLIMIT_RSS
44 #define CAN_USE_RLIMIT_CPU
45
46 int main (int argc, char *argv[] )
47 {
48 struct rusage kid_usage;
49 pid_t kid;
50 int kid_status;
51 int i, opt, echo_args = 0, exit_flag;
52 long int sample_time=0, time = 0;
53
54 int maxkbytes=0; //kilobytes
55 int maxseconds=0; //seconds
56 long int maxmillis=0;
57
58 unsigned int max_vsize = 0, max_rss = 0;
59 unsigned int start, end;
60
61 struct memtime_info info;
62 // struct rlimit currentl;
63
64 if (argc < 2) {
65 char *tmp = strrchr(argv[0], '/');
66 tmp = (tmp ? tmp + 1 : argv[0]);
67
68 fprintf(stderr,
69 "%s: usage %s [-t <interval>] [-e] [-m <maxkilobytes>] [-c <maxcpuseconds>] <cmd> [<params>]\n",
70 tmp,tmp);
71 exit(EXIT_FAILURE);
72 }
73
74 while ((opt = getopt(argc, argv, "+et:m:c:")) != -1) {
75
76 switch (opt) {
77 case 'e' :
78 echo_args = 1;
79 break;
80
81 case 't' :
82 errno = 0;
83 sample_time = strtol(optarg, NULL, 0);
84 if (errno) {
85 perror("Illegal argument to t option");
86 exit(EXIT_FAILURE);
87 }
88 break;
89 case 'm' :
90 errno = 0;
91 maxkbytes = atoi(optarg);
92 if (errno) {
93 perror("Illegal argument to m option");
94 exit(EXIT_FAILURE);
95 }
96 break;
97
98 case 'c' :
99 errno = 0;
100 maxseconds = atoi(optarg);
101 if (errno) {
102 perror("Illegal argument to c option");
103 exit(EXIT_FAILURE);
104 }
105 maxmillis=1000*maxseconds;
106 break;
107
108 }
109 }
110
111 if (echo_args) {
112 fprintf(stderr,"Command line: ");
113 for (i = optind; i < argc; i++)
114 fprintf(stderr,"%s ", argv[i]);
115 fprintf(stderr,"\n");
116 }
117
118 start = get_time();
119
120 switch (kid = fork()) {
121
122 case -1 :
123 perror("fork failed");
124 exit(EXIT_FAILURE);
125
126 case 0 :
127 #if defined(CAN_USE_RLIMIT_RSS)
128 if (maxkbytes>0) {
129 set_mem_limit((long int)maxkbytes*1024);
130 }
131 #endif
132 #if defined(CAN_USE_RLIMIT_CPU)
133 if (maxseconds>0) {
134 set_cpu_limit((long int)maxseconds);
135 }
136 #endif
137 execvp(argv[optind], &(argv[optind]));
138 perror("exec failed");
139 exit(EXIT_FAILURE);
140
141 default :
142 break;
143 }
144
145 if (!init_machdep(kid)) {
146 fprintf(stderr, "%s: Failed to initialise sampling.\n", argv[0]);
147 exit(EXIT_FAILURE);
148 }
149
150 do {
151
152 get_sample(&info);
153
154 max_vsize = (info.vsize_kb > max_vsize ? info.vsize_kb : max_vsize);
155 max_rss = (info.rss_kb > max_rss ? info.rss_kb : max_rss);
156
157 if (sample_time) {
158 time++;
159 if (time == 10 * sample_time) {
160 end = get_time();
161
162 fprintf(stderr,"%.2f user, %.2f system, %.2f elapsed"
163 " -- VSize = %dKB, RSS = %dKB\n",
164 (double)info.utime_ms/1000.0,
165 (double)info.stime_ms/1000.0,
166 (double)(end - start)/1000.0,
167 info.vsize_kb, info.rss_kb);
168 fflush(stdout);
169
170 time = 1;
171 }
172 }
173
174 usleep(100000);
175
176 exit_flag = ((wait4(kid, &kid_status, WNOHANG, &kid_usage) == kid)
177 && (WIFEXITED(kid_status) || WIFSIGNALED(kid_status)));
178 #if !defined(CAN_USE_RLIMIT_RSS)
179 if ((maxkbytes>0) && (max_vsize>maxkbytes)) {
180 kill(kid,SIGKILL);
181 }
182 #endif
183 #if !defined(CAN_USE_RLIMIT_CPU)
184 if ((maxmillis>0) && (info.utime_ms>maxmillis)) {
185 kill(kid,SIGKILL);
186 }
187 #endif
188 } while (!exit_flag);
189
190 end = get_time();
191
192 if (WIFEXITED(kid_status)) {
193 fprintf(stderr, "Exit [%d]\n", WEXITSTATUS(kid_status));
194 } else {
195 fprintf(stderr, "Killed [%d]\n", WTERMSIG(kid_status));
196 }
197
198 {
199 double kid_utime = ((double)kid_usage.ru_utime.tv_sec
200 + (double)kid_usage.ru_utime.tv_usec / 1E6);
201 double kid_stime = ((double)kid_usage.ru_stime.tv_sec
202 + (double)kid_usage.ru_stime.tv_usec / 1E6);
203
204 fprintf(stderr, "%.2f user, %.2f system, %.2f elapsed -- "
205 "Max VSize = %dKB, Max RSS = %dKB\n",
206 kid_utime, kid_stime, (double)(end - start) / 1000.0,
207 max_vsize, max_rss);
208 }
209
210 exit(EXIT_SUCCESS);
211 }
212