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 #!/usr/bin/ruby
1 #!/usr/bin/ruby
2
2
3 # new_problem:
3 # new_problem:
4 # * creates a directory for a problem in the current directory,
4 # * creates a directory for a problem in the current directory,
5 # * create standard testcase config file
5 # * create standard testcase config file
6
6
7 require 'erb'
7 require 'erb'
8
8
9 def process_options(options)
9 def process_options(options)
10 i = 2
10 i = 2
11 while i<ARGV.length
11 while i<ARGV.length
12 if ARGV[i]=='-t'
12 if ARGV[i]=='-t'
13 options[:time_limit] = ARGV[i+1].to_i if ARGV.length>i+1
13 options[:time_limit] = ARGV[i+1].to_i if ARGV.length>i+1
14 i += 1
14 i += 1
15 end
15 end
16 if ARGV[i]=='-m'
16 if ARGV[i]=='-m'
17 options[:mem_limit] = ARGV[i+1].to_i if ARGV.length>i+1
17 options[:mem_limit] = ARGV[i+1].to_i if ARGV.length>i+1
18 i += 1
18 i += 1
19 end
19 end
20 i += 1
20 i += 1
21 end
21 end
22 end
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 GRADER_DIR = File.dirname(__FILE__)
29 GRADER_DIR = File.dirname(__FILE__)
25
30
26 # print usage
31 # print usage
27 if ARGV.length < 2
32 if ARGV.length < 2
28 puts <<USAGE
33 puts <<USAGE
29 using: new_problem problem number_of_testcase [options]
34 using: new_problem problem number_of_testcase [options]
30 * creates a directory for a problem in the current directory,
35 * creates a directory for a problem in the current directory,
31 * create standard testcase config file
36 * create standard testcase config file
32 * options: -t time-limit (in seconds)
37 * options: -t time-limit (in seconds)
33 -m mem-limit (in MB)
38 -m mem-limit (in MB)
34 USAGE
39 USAGE
35 exit(127)
40 exit(127)
36 end
41 end
37
42
38 # processing arguments
43 # processing arguments
39 problem = ARGV[0]
44 problem = ARGV[0]
40 num_testcases = ARGV[1].to_i
45 num_testcases = ARGV[1].to_i
41
46
42 options = {:time_limit => 1, :mem_limit => 16}
47 options = {:time_limit => 1, :mem_limit => 16}
43 process_options(options)
48 process_options(options)
44
49
45 # start working
50 # start working
46 puts "creating directories"
51 puts "creating directories"
47
52
48 system("mkdir #{problem}")
53 system("mkdir #{problem}")
49 system("mkdir #{problem}/script")
54 system("mkdir #{problem}/script")
50 system("mkdir #{problem}/test_cases")
55 system("mkdir #{problem}/test_cases")
51
56
52 puts "creating testcases directories"
57 puts "creating testcases directories"
53
58
54 1.upto(num_testcases) do |i|
59 1.upto(num_testcases) do |i|
55 system("mkdir #{problem}/test_cases/#{i}")
60 system("mkdir #{problem}/test_cases/#{i}")
56 end
61 end
57
62
58 # generating all_tests.cfg
63 # generating all_tests.cfg
59 puts "generating testcase config file"
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 all_test_cfg = ERB.new(template)
67 all_test_cfg = ERB.new(template)
63
68
64 cfg_file = File.open("#{problem}/test_cases/all_tests.cfg","w")
69 cfg_file = File.open("#{problem}/test_cases/all_tests.cfg","w")
65 cfg_file.puts all_test_cfg.result
70 cfg_file.puts all_test_cfg.result
66 cfg_file.close
71 cfg_file.close
67
72
68 puts "done"
73 puts "done"
@@ -23,48 +23,49
23 #include <sys/signal.h>
23 #include <sys/signal.h>
24 #include <sys/sysinfo.h>
24 #include <sys/sysinfo.h>
25 #include <sys/syscall.h>
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
26 #include <sys/resource.h>
27
27
28 #define NONRET __attribute__((noreturn))
28 #define NONRET __attribute__((noreturn))
29 #define UNUSED __attribute__((unused))
29 #define UNUSED __attribute__((unused))
30
30
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
32 static int timeout;
32 static int timeout;
33 static int pass_environ;
33 static int pass_environ;
34 static int use_wall_clock;
34 static int use_wall_clock;
35 static int file_access;
35 static int file_access;
36 static int verbose;
36 static int verbose;
37 static int memory_limit;
37 static int memory_limit;
38 static int allow_times;
38 static int allow_times;
39 static char *redir_stdin, *redir_stdout;
39 static char *redir_stdin, *redir_stdout;
40 static char *set_cwd;
40 static char *set_cwd;
41
41
42 static pid_t box_pid;
42 static pid_t box_pid;
43 static int is_ptraced;
43 static int is_ptraced;
44 static volatile int timer_tick;
44 static volatile int timer_tick;
45 static time_t start_time;
45 static time_t start_time;
46 static int ticks_per_sec;
46 static int ticks_per_sec;
47 + static int page_size;
47
48
48 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
49 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
49 /* glibc 2.1 or newer -> has lseek64 */
50 /* glibc 2.1 or newer -> has lseek64 */
50 #define long_seek(f,o,w) lseek64(f,o,w)
51 #define long_seek(f,o,w) lseek64(f,o,w)
51 #else
52 #else
52 /* Touching clandestine places in glibc */
53 /* Touching clandestine places in glibc */
53 extern loff_t llseek(int fd, loff_t pos, int whence);
54 extern loff_t llseek(int fd, loff_t pos, int whence);
54 #define long_seek(f,o,w) llseek(f,o,w)
55 #define long_seek(f,o,w) llseek(f,o,w)
55 #endif
56 #endif
56
57
57 int max_mem_used = 0;
58 int max_mem_used = 0;
58
59
59 void print_running_stat(double wall_time,
60 void print_running_stat(double wall_time,
60 double user_time,
61 double user_time,
61 double system_time,
62 double system_time,
62 int mem_usage)
63 int mem_usage)
63 {
64 {
64 fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
65 fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
65 wall_time, user_time, system_time, mem_usage);
66 wall_time, user_time, system_time, mem_usage);
66 }
67 }
67
68
68 static void NONRET
69 static void NONRET
69 box_exit(void)
70 box_exit(void)
70 {
71 {
@@ -363,68 +364,71
363 die("proc syntax error 2");
364 die("proc syntax error 2");
364 //printf("%s - %d\n",x,ticks_per_sec);
365 //printf("%s - %d\n",x,ticks_per_sec);
365 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
366 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
366 }
367 }
367 if (verbose > 1)
368 if (verbose > 1)
368 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
369 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
369 if (sec > timeout) {
370 if (sec > timeout) {
370 die("Time limit exceeded.");
371 die("Time limit exceeded.");
371 }
372 }
372 }
373 }
373
374
374 static void
375 static void
375 check_memory_usage()
376 check_memory_usage()
376 {
377 {
377 char proc_fname[100];
378 char proc_fname[100];
378 sprintf(proc_fname,"/proc/%d/statm",box_pid);
379 sprintf(proc_fname,"/proc/%d/statm",box_pid);
379 //printf("proc fname: %s\n",proc_fname);
380 //printf("proc fname: %s\n",proc_fname);
380 FILE *fp = fopen(proc_fname,"r");
381 FILE *fp = fopen(proc_fname,"r");
381 if(fp!=NULL) {
382 if(fp!=NULL) {
382 char line[1000];
383 char line[1000];
383 fgets(line,999,fp);
384 fgets(line,999,fp);
384 //printf("%s\n",line);
385 //printf("%s\n",line);
385 int m;
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 if(m>max_mem_used)
390 if(m>max_mem_used)
389 max_mem_used = m;
391 max_mem_used = m;
392 + }
390
393
391 fclose(fp);
394 fclose(fp);
392 }
395 }
393 }
396 }
394
397
395 static void
398 static void
396 boxkeeper(void)
399 boxkeeper(void)
397 {
400 {
398 int syscall_count = 0;
401 int syscall_count = 0;
399 struct sigaction sa;
402 struct sigaction sa;
400
403
401 is_ptraced = 1;
404 is_ptraced = 1;
402 bzero(&sa, sizeof(sa));
405 bzero(&sa, sizeof(sa));
403 sa.sa_handler = signal_int;
406 sa.sa_handler = signal_int;
404 sigaction(SIGINT, &sa, NULL);
407 sigaction(SIGINT, &sa, NULL);
405 start_time = time(NULL);
408 start_time = time(NULL);
406 ticks_per_sec = sysconf(_SC_CLK_TCK);
409 ticks_per_sec = sysconf(_SC_CLK_TCK);
410 + page_size = getpagesize();
407 if (ticks_per_sec <= 0)
411 if (ticks_per_sec <= 0)
408 die("Invalid ticks_per_sec!");
412 die("Invalid ticks_per_sec!");
409
413
410 check_memory_usage();
414 check_memory_usage();
411
415
412 sa.sa_handler = signal_alarm;
416 sa.sa_handler = signal_alarm;
413 sigaction(SIGALRM, &sa, NULL);
417 sigaction(SIGALRM, &sa, NULL);
414 //alarm(1);
418 //alarm(1);
415
419
416 struct itimerval val;
420 struct itimerval val;
417 val.it_interval.tv_sec = 0;
421 val.it_interval.tv_sec = 0;
418 val.it_interval.tv_usec = 50000;
422 val.it_interval.tv_usec = 50000;
419 val.it_value.tv_sec = 0;
423 val.it_value.tv_sec = 0;
420 val.it_value.tv_usec = 50000;
424 val.it_value.tv_usec = 50000;
421 setitimer(ITIMER_REAL,&val,NULL);
425 setitimer(ITIMER_REAL,&val,NULL);
422
426
423 /*
427 /*
424 --- add alarm handler no matter what..
428 --- add alarm handler no matter what..
425 if (timeout)
429 if (timeout)
426 {
430 {
427 sa.sa_handler = signal_alarm;
431 sa.sa_handler = signal_alarm;
428 sigaction(SIGALRM, &sa, NULL);
432 sigaction(SIGALRM, &sa, NULL);
429 alarm(1);
433 alarm(1);
430 }
434 }
You need to be logged in to leave comments. Login now