Description:
[grader] obsolete new_problem, fixed memory measurement bug in box.cc git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@186 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r49:5746784ead66 - - 2 files changed: 11 inserted, 2 deleted

@@ -1,68 +1,73
1 1 #!/usr/bin/ruby
2 2
3 3 # new_problem:
4 4 # * creates a directory for a problem in the current directory,
5 5 # * create standard testcase config file
6 6
7 7 require 'erb'
8 8
9 9 def process_options(options)
10 10 i = 2
11 11 while i<ARGV.length
12 12 if ARGV[i]=='-t'
13 13 options[:time_limit] = ARGV[i+1].to_i if ARGV.length>i+1
14 14 i += 1
15 15 end
16 16 if ARGV[i]=='-m'
17 17 options[:mem_limit] = ARGV[i+1].to_i if ARGV.length>i+1
18 18 i += 1
19 19 end
20 20 i += 1
21 21 end
22 22 end
23 23
24 +
25 + puts "This script is out of dated, shall be fixed soon"
26 + puts "Right now, you can create raw_ev and import"
27 + exit(0)
28 +
24 29 GRADER_DIR = File.dirname(__FILE__)
25 30
26 31 # print usage
27 32 if ARGV.length < 2
28 33 puts <<USAGE
29 34 using: new_problem problem number_of_testcase [options]
30 35 * creates a directory for a problem in the current directory,
31 36 * create standard testcase config file
32 37 * options: -t time-limit (in seconds)
33 38 -m mem-limit (in MB)
34 39 USAGE
35 40 exit(127)
36 41 end
37 42
38 43 # processing arguments
39 44 problem = ARGV[0]
40 45 num_testcases = ARGV[1].to_i
41 46
42 47 options = {:time_limit => 1, :mem_limit => 16}
43 48 process_options(options)
44 49
45 50 # start working
46 51 puts "creating directories"
47 52
48 53 system("mkdir #{problem}")
49 54 system("mkdir #{problem}/script")
50 55 system("mkdir #{problem}/test_cases")
51 56
52 57 puts "creating testcases directories"
53 58
54 59 1.upto(num_testcases) do |i|
55 60 system("mkdir #{problem}/test_cases/#{i}")
56 61 end
57 62
58 63 # generating all_tests.cfg
59 64 puts "generating testcase config file"
60 65
61 - template = File.open(File.dirname(__FILE__) + "/all_tests.cfg.erb").read
66 + template = File.open(File.dirname(__FILE__) + "/templates/all_tests.cfg.erb").read
62 67 all_test_cfg = ERB.new(template)
63 68
64 69 cfg_file = File.open("#{problem}/test_cases/all_tests.cfg","w")
65 70 cfg_file.puts all_test_cfg.result
66 71 cfg_file.close
67 72
68 73 puts "done"
@@ -1,142 +1,143
1 1 /*
2 2 * A Simple Testing Sandbox
3 3 *
4 4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
5 5 */
6 6
7 7 #define _LARGEFILE64_SOURCE
8 8 //#define _GNU_SOURCE
9 9
10 10 #include <errno.h>
11 11 #include <stdio.h>
12 12 #include <fcntl.h>
13 13 #include <stdlib.h>
14 14 #include <string.h>
15 15 #include <stdarg.h>
16 16 #include <unistd.h>
17 17 #include <getopt.h>
18 18 #include <time.h>
19 19 #include <sys/wait.h>
20 20 #include <sys/user.h>
21 21 #include <sys/time.h>
22 22 #include <sys/ptrace.h>
23 23 #include <sys/signal.h>
24 24 #include <sys/sysinfo.h>
25 25 #include <sys/syscall.h>
26 26 #include <sys/resource.h>
27 27
28 28 #define NONRET __attribute__((noreturn))
29 29 #define UNUSED __attribute__((unused))
30 30
31 31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
32 32 static int timeout;
33 33 static int pass_environ;
34 34 static int use_wall_clock;
35 35 static int file_access;
36 36 static int verbose;
37 37 static int memory_limit;
38 38 static int allow_times;
39 39 static char *redir_stdin, *redir_stdout;
40 40 static char *set_cwd;
41 41
42 42 static pid_t box_pid;
43 43 static int is_ptraced;
44 44 static volatile int timer_tick;
45 45 static time_t start_time;
46 46 static int ticks_per_sec;
47 + static int page_size;
47 48
48 49 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
49 50 /* glibc 2.1 or newer -> has lseek64 */
50 51 #define long_seek(f,o,w) lseek64(f,o,w)
51 52 #else
52 53 /* Touching clandestine places in glibc */
53 54 extern loff_t llseek(int fd, loff_t pos, int whence);
54 55 #define long_seek(f,o,w) llseek(f,o,w)
55 56 #endif
56 57
57 58 int max_mem_used = 0;
58 59
59 60 void print_running_stat(double wall_time,
60 61 double user_time,
61 62 double system_time,
62 63 int mem_usage)
63 64 {
64 65 fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
65 66 wall_time, user_time, system_time, mem_usage);
66 67 }
67 68
68 69 static void NONRET
69 70 box_exit(void)
70 71 {
71 72 if (box_pid > 0) {
72 73 if (is_ptraced)
73 74 ptrace(PTRACE_KILL, box_pid);
74 75 kill(-box_pid, SIGKILL);
75 76 kill(box_pid, SIGKILL);
76 77 }
77 78
78 79 struct timeval total;
79 80 int wall;
80 81 struct rusage rus;
81 82 int stat;
82 83 pid_t p;
83 84
84 85 // wait so that we can get information
85 86 p = wait4(box_pid, &stat, WUNTRACED, &rus);
86 87 if (p < 0) {
87 88 fprintf(stderr,"wait4: error\n");
88 89 print_running_stat(0,0,0,max_mem_used);
89 90 } else if (p != box_pid) {
90 91 fprintf(stderr,"wait4: unknown pid %d exited!\n", p);
91 92 print_running_stat(0,0,0,max_mem_used);
92 93 } else {
93 94 if (!WIFEXITED(stat))
94 95 fprintf(stderr,"wait4: unknown status\n");
95 96 struct timeval total;
96 97 int wall;
97 98 wall = time(NULL) - start_time;
98 99 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
99 100
100 101 print_running_stat((double)wall,
101 102 (double) rus.ru_utime.tv_sec +
102 103 ((double) rus.ru_utime.tv_usec/1000000.0),
103 104 (double) rus.ru_stime.tv_sec +
104 105 ((double) rus.ru_stime.tv_usec/1000000.0),
105 106 max_mem_used);
106 107 }
107 108 exit(1);
108 109 }
109 110
110 111 static void NONRET __attribute__((format(printf,1,2)))
111 112 die(char *msg, ...)
112 113 {
113 114 va_list args;
114 115 va_start(args, msg);
115 116 vfprintf(stderr, msg, args);
116 117 fputc('\n', stderr);
117 118 box_exit();
118 119 }
119 120
120 121 static void __attribute__((format(printf,1,2)))
121 122 log(char *msg, ...)
122 123 {
123 124 va_list args;
124 125 va_start(args, msg);
125 126 if (verbose)
126 127 {
127 128 vfprintf(stderr, msg, args);
128 129 fflush(stderr);
129 130 }
130 131 va_end(args);
131 132 }
132 133
133 134 static void
134 135 valid_filename(unsigned long addr)
135 136 {
136 137 char namebuf[4096], *p, *end;
137 138 static int mem_fd;
138 139
139 140 if (!file_access)
140 141 die("File access forbidden.");
141 142 if (file_access >= 9)
142 143 return;
@@ -291,212 +292,215
291 292 case __NR_rt_sigpending:
292 293 case __NR_rt_sigtimedwait:
293 294 case __NR_rt_sigqueueinfo:
294 295 case __NR_rt_sigsuspend:
295 296 case __NR_mmap2:
296 297 case __NR__sysctl:
297 298 return (filter_syscalls == 1);
298 299 case __NR_times:
299 300 return allow_times;
300 301 case __NR_kill:
301 302 if (u->regs.ebx == box_pid)
302 303 die("Commited suicide by signal %d.", (int)u->regs.ecx);
303 304 return 0;
304 305 default:
305 306 return 0;
306 307 }
307 308 }
308 309
309 310 static void
310 311 signal_alarm(int unused UNUSED)
311 312 {
312 313 /* Time limit checks are synchronous, so we only schedule them there. */
313 314 timer_tick = 1;
314 315
315 316 //NOTE: do not use alarm, changed to setitimer for precision
316 317 // alarm(1);
317 318 }
318 319
319 320 static void
320 321 signal_int(int unused UNUSED)
321 322 {
322 323 /* Interrupts are fatal, so no synchronization requirements. */
323 324 die("Interrupted.");
324 325 }
325 326
326 327 static void
327 328 check_timeout(void)
328 329 {
329 330 int sec;
330 331
331 332 if (use_wall_clock)
332 333 sec = time(NULL) - start_time;
333 334 else
334 335 {
335 336 char buf[4096], *x;
336 337 int c, utime, stime;
337 338 static int proc_status_fd;
338 339 if (!proc_status_fd)
339 340 {
340 341 sprintf(buf, "/proc/%d/stat", (int) box_pid);
341 342 proc_status_fd = open(buf, O_RDONLY);
342 343 if (proc_status_fd < 0)
343 344 die("open(%s): %m", buf);
344 345 }
345 346 lseek(proc_status_fd, 0, SEEK_SET);
346 347 if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
347 348 die("read on /proc/$pid/stat: %m");
348 349 if (c >= (int) sizeof(buf) - 1)
349 350 die("/proc/$pid/stat too long");
350 351 buf[c] = 0;
351 352 x = buf;
352 353 while (*x && *x != ' ')
353 354 x++;
354 355 while (*x == ' ')
355 356 x++;
356 357 if (*x++ != '(')
357 358 die("proc syntax error 1");
358 359 while (*x && (*x != ')' || x[1] != ' '))
359 360 x++;
360 361 while (*x == ')' || *x == ' ')
361 362 x++;
362 363 if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
363 364 die("proc syntax error 2");
364 365 //printf("%s - %d\n",x,ticks_per_sec);
365 366 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
366 367 }
367 368 if (verbose > 1)
368 369 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
369 370 if (sec > timeout) {
370 371 die("Time limit exceeded.");
371 372 }
372 373 }
373 374
374 375 static void
375 376 check_memory_usage()
376 377 {
377 378 char proc_fname[100];
378 379 sprintf(proc_fname,"/proc/%d/statm",box_pid);
379 380 //printf("proc fname: %s\n",proc_fname);
380 381 FILE *fp = fopen(proc_fname,"r");
381 382 if(fp!=NULL) {
382 383 char line[1000];
383 384 fgets(line,999,fp);
384 385 //printf("%s\n",line);
385 386 int m;
386 387
387 - if(sscanf(line,"%d",&m)==1)
388 + if(sscanf(line,"%d",&m)==1) {
389 + m = (m*page_size+1023)/1024;
388 390 if(m>max_mem_used)
389 391 max_mem_used = m;
392 + }
390 393
391 394 fclose(fp);
392 395 }
393 396 }
394 397
395 398 static void
396 399 boxkeeper(void)
397 400 {
398 401 int syscall_count = 0;
399 402 struct sigaction sa;
400 403
401 404 is_ptraced = 1;
402 405 bzero(&sa, sizeof(sa));
403 406 sa.sa_handler = signal_int;
404 407 sigaction(SIGINT, &sa, NULL);
405 408 start_time = time(NULL);
406 409 ticks_per_sec = sysconf(_SC_CLK_TCK);
410 + page_size = getpagesize();
407 411 if (ticks_per_sec <= 0)
408 412 die("Invalid ticks_per_sec!");
409 413
410 414 check_memory_usage();
411 415
412 416 sa.sa_handler = signal_alarm;
413 417 sigaction(SIGALRM, &sa, NULL);
414 418 //alarm(1);
415 419
416 420 struct itimerval val;
417 421 val.it_interval.tv_sec = 0;
418 422 val.it_interval.tv_usec = 50000;
419 423 val.it_value.tv_sec = 0;
420 424 val.it_value.tv_usec = 50000;
421 425 setitimer(ITIMER_REAL,&val,NULL);
422 426
423 427 /*
424 428 --- add alarm handler no matter what..
425 429 if (timeout)
426 430 {
427 431 sa.sa_handler = signal_alarm;
428 432 sigaction(SIGALRM, &sa, NULL);
429 433 alarm(1);
430 434 }
431 435 */
432 436
433 437 for(;;)
434 438 {
435 439 struct rusage rus;
436 440 int stat;
437 441 pid_t p;
438 442
439 443 if (timer_tick)
440 444 {
441 445 check_timeout();
442 446 check_memory_usage();
443 447 timer_tick = 0;
444 448 }
445 449 p = wait4(box_pid, &stat, WUNTRACED, &rus);
446 450
447 451 if (p < 0)
448 452 {
449 453 if (errno == EINTR)
450 454 continue;
451 455 die("wait4: %m");
452 456 }
453 457 if (p != box_pid)
454 458 die("wait4: unknown pid %d exited!", p);
455 459 if (WIFEXITED(stat))
456 460 {
457 461 struct timeval total;
458 462 int wall;
459 463 wall = time(NULL) - start_time;
460 464 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
461 465
462 466 box_pid = 0;
463 467 if (WEXITSTATUS(stat))
464 468 fprintf(stderr,"Exited with error status %d.\n", WEXITSTATUS(stat));
465 469 else if ((use_wall_clock ? wall : total.tv_sec) > timeout)
466 470 fprintf(stderr,"Time limit exceeded.\n");
467 471 else
468 472 // report OK and statistics
469 473 fprintf(stderr,"OK\n");
470 474
471 475 print_running_stat((double) wall,
472 476 (double) rus.ru_utime.tv_sec +
473 477 ((double) rus.ru_utime.tv_usec/1000000.0),
474 478 (double) rus.ru_stime.tv_sec +
475 479 ((double) rus.ru_stime.tv_usec/1000000.0),
476 480 max_mem_used);
477 481 /*
478 482 (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n",
479 483 (double) total.tv_sec + ((double)total.tv_usec / 1000000.0),
480 484 (int) total.tv_usec,
481 485 wall,
482 486 syscall_count,
483 487 max_mem_used);
484 488 */
485 489 exit(0);
486 490 }
487 491 if (WIFSIGNALED(stat))
488 492 {
489 493 box_pid = 0;
490 494 fprintf(stderr,"Caught fatal signal %d.\n", WTERMSIG(stat));
491 495
492 496 struct timeval total;
493 497 int wall;
494 498 wall = time(NULL) - start_time;
495 499 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
496 500 print_running_stat((double) wall,
497 501 (double) rus.ru_utime.tv_sec +
498 502 ((double) rus.ru_utime.tv_usec/1000000.0),
499 503 (double) rus.ru_stime.tv_sec +
500 504 ((double) rus.ru_stime.tv_usec/1000000.0),
501 505 max_mem_used);
502 506 exit(0);
You need to be logged in to leave comments. Login now