Description:
[grader] better test case for timeout, changed where check_mem_usage is called to reduce inefficiency
git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@167 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r43:b03203ec084a - - 2 files changed: 25 inserted, 11 deleted
@@ -330,246 +330,242 | |||||
|
330 | if (c >= (int) sizeof(buf) - 1) |
|
330 | if (c >= (int) sizeof(buf) - 1) |
|
331 | die("/proc/$pid/stat too long"); |
|
331 | die("/proc/$pid/stat too long"); |
|
332 | buf[c] = 0; |
|
332 | buf[c] = 0; |
|
333 | x = buf; |
|
333 | x = buf; |
|
334 | while (*x && *x != ' ') |
|
334 | while (*x && *x != ' ') |
|
335 | x++; |
|
335 | x++; |
|
336 | while (*x == ' ') |
|
336 | while (*x == ' ') |
|
337 | x++; |
|
337 | x++; |
|
338 | if (*x++ != '(') |
|
338 | if (*x++ != '(') |
|
339 | die("proc syntax error 1"); |
|
339 | die("proc syntax error 1"); |
|
340 | while (*x && (*x != ')' || x[1] != ' ')) |
|
340 | while (*x && (*x != ')' || x[1] != ' ')) |
|
341 | x++; |
|
341 | x++; |
|
342 | while (*x == ')' || *x == ' ') |
|
342 | while (*x == ')' || *x == ' ') |
|
343 | x++; |
|
343 | x++; |
|
344 | if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2) |
|
344 | if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2) |
|
345 | die("proc syntax error 2"); |
|
345 | die("proc syntax error 2"); |
|
346 | //printf("%s - %d\n",x,ticks_per_sec); |
|
346 | //printf("%s - %d\n",x,ticks_per_sec); |
|
347 | sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec; |
|
347 | sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec; |
|
348 | } |
|
348 | } |
|
349 | if (verbose > 1) |
|
349 | if (verbose > 1) |
|
350 | fprintf(stderr, "[timecheck: %d seconds]\n", sec); |
|
350 | fprintf(stderr, "[timecheck: %d seconds]\n", sec); |
|
351 | if (sec > timeout) { |
|
351 | if (sec > timeout) { |
|
352 | die_report("Time limit exceeded."); |
|
352 | die_report("Time limit exceeded."); |
|
353 | } |
|
353 | } |
|
354 | } |
|
354 | } |
|
355 |
|
355 | ||
|
356 | int max_mem_used = 0; |
|
356 | int max_mem_used = 0; |
|
357 |
|
357 | ||
|
358 | static void |
|
358 | static void |
|
359 | check_memory_usage() |
|
359 | check_memory_usage() |
|
360 | { |
|
360 | { |
|
361 | char proc_fname[100]; |
|
361 | char proc_fname[100]; |
|
362 | sprintf(proc_fname,"/proc/%d/statm",box_pid); |
|
362 | sprintf(proc_fname,"/proc/%d/statm",box_pid); |
|
363 | //printf("proc fname: %s\n",proc_fname); |
|
363 | //printf("proc fname: %s\n",proc_fname); |
|
364 | FILE *fp = fopen(proc_fname,"r"); |
|
364 | FILE *fp = fopen(proc_fname,"r"); |
|
365 | if(fp!=NULL) { |
|
365 | if(fp!=NULL) { |
|
366 | char line[1000]; |
|
366 | char line[1000]; |
|
367 | fgets(line,999,fp); |
|
367 | fgets(line,999,fp); |
|
368 | //printf("%s\n",line); |
|
368 | //printf("%s\n",line); |
|
369 | int m; |
|
369 | int m; |
|
370 |
|
370 | ||
|
371 | if(sscanf(line,"%d",&m)==1) |
|
371 | if(sscanf(line,"%d",&m)==1) |
|
372 | if(m>max_mem_used) |
|
372 | if(m>max_mem_used) |
|
373 | max_mem_used = m; |
|
373 | max_mem_used = m; |
|
374 |
|
374 | ||
|
375 | fclose(fp); |
|
375 | fclose(fp); |
|
376 | } |
|
376 | } |
|
377 | } |
|
377 | } |
|
378 |
|
378 | ||
|
379 | static void |
|
379 | static void |
|
380 | boxkeeper(void) |
|
380 | boxkeeper(void) |
|
381 | { |
|
381 | { |
|
382 | int syscall_count = 0; |
|
382 | int syscall_count = 0; |
|
383 | struct sigaction sa; |
|
383 | struct sigaction sa; |
|
384 |
|
384 | ||
|
385 | is_ptraced = 1; |
|
385 | is_ptraced = 1; |
|
386 | bzero(&sa, sizeof(sa)); |
|
386 | bzero(&sa, sizeof(sa)); |
|
387 | sa.sa_handler = signal_int; |
|
387 | sa.sa_handler = signal_int; |
|
388 | sigaction(SIGINT, &sa, NULL); |
|
388 | sigaction(SIGINT, &sa, NULL); |
|
389 | start_time = time(NULL); |
|
389 | start_time = time(NULL); |
|
390 | ticks_per_sec = sysconf(_SC_CLK_TCK); |
|
390 | ticks_per_sec = sysconf(_SC_CLK_TCK); |
|
391 | if (ticks_per_sec <= 0) |
|
391 | if (ticks_per_sec <= 0) |
|
392 | die("Invalid ticks_per_sec!"); |
|
392 | die("Invalid ticks_per_sec!"); |
|
393 |
|
393 | ||
|
394 | check_memory_usage(); |
|
394 | check_memory_usage(); |
|
395 |
|
395 | ||
|
396 | sa.sa_handler = signal_alarm; |
|
396 | sa.sa_handler = signal_alarm; |
|
397 | sigaction(SIGALRM, &sa, NULL); |
|
397 | sigaction(SIGALRM, &sa, NULL); |
|
398 | //alarm(1); |
|
398 | //alarm(1); |
|
399 |
|
399 | ||
|
400 | struct itimerval val; |
|
400 | struct itimerval val; |
|
401 | val.it_interval.tv_sec = 0; |
|
401 | val.it_interval.tv_sec = 0; |
|
402 | val.it_interval.tv_usec = 50000; |
|
402 | val.it_interval.tv_usec = 50000; |
|
403 | val.it_value.tv_sec = 0; |
|
403 | val.it_value.tv_sec = 0; |
|
404 | val.it_value.tv_usec = 50000; |
|
404 | val.it_value.tv_usec = 50000; |
|
405 | setitimer(ITIMER_REAL,&val,NULL); |
|
405 | setitimer(ITIMER_REAL,&val,NULL); |
|
406 |
|
406 | ||
|
407 | /* |
|
407 | /* |
|
408 | --- add alarm handler no matter what.. |
|
408 | --- add alarm handler no matter what.. |
|
409 | if (timeout) |
|
409 | if (timeout) |
|
410 | { |
|
410 | { |
|
411 | sa.sa_handler = signal_alarm; |
|
411 | sa.sa_handler = signal_alarm; |
|
412 | sigaction(SIGALRM, &sa, NULL); |
|
412 | sigaction(SIGALRM, &sa, NULL); |
|
413 | alarm(1); |
|
413 | alarm(1); |
|
414 | } |
|
414 | } |
|
415 | */ |
|
415 | */ |
|
416 |
|
416 | ||
|
417 | for(;;) |
|
417 | for(;;) |
|
418 | { |
|
418 | { |
|
419 | struct rusage rus; |
|
419 | struct rusage rus; |
|
420 | int stat; |
|
420 | int stat; |
|
421 | pid_t p; |
|
421 | pid_t p; |
|
422 |
|
422 | ||
|
423 | if (timer_tick) |
|
423 | if (timer_tick) |
|
424 | { |
|
424 | { |
|
425 | check_timeout(); |
|
425 | check_timeout(); |
|
|
426 | + check_memory_usage(); | ||
|
426 | timer_tick = 0; |
|
427 | timer_tick = 0; |
|
427 | } |
|
428 | } |
|
428 | p = wait4(box_pid, &stat, WUNTRACED, &rus); |
|
429 | p = wait4(box_pid, &stat, WUNTRACED, &rus); |
|
429 |
|
430 | ||
|
430 | - if(!WIFEXITED(stat)) { |
|
||
|
431 | - // printf("CHECKING\n"); |
|
||
|
432 | - check_memory_usage(); |
|
||
|
433 | - } |
|
||
|
434 | - |
|
||
|
435 | if (p < 0) |
|
431 | if (p < 0) |
|
436 | { |
|
432 | { |
|
437 | if (errno == EINTR) |
|
433 | if (errno == EINTR) |
|
438 | continue; |
|
434 | continue; |
|
439 | die("wait4: %m"); |
|
435 | die("wait4: %m"); |
|
440 | } |
|
436 | } |
|
441 | if (p != box_pid) |
|
437 | if (p != box_pid) |
|
442 | die("wait4: unknown pid %d exited!", p); |
|
438 | die("wait4: unknown pid %d exited!", p); |
|
443 | if (WIFEXITED(stat)) |
|
439 | if (WIFEXITED(stat)) |
|
444 | { |
|
440 | { |
|
445 | struct timeval total; |
|
441 | struct timeval total; |
|
446 | int wall; |
|
442 | int wall; |
|
447 | wall = time(NULL) - start_time; |
|
443 | wall = time(NULL) - start_time; |
|
448 | timeradd(&rus.ru_utime, &rus.ru_stime, &total); |
|
444 | timeradd(&rus.ru_utime, &rus.ru_stime, &total); |
|
449 |
|
445 | ||
|
450 | box_pid = 0; |
|
446 | box_pid = 0; |
|
451 | if (WEXITSTATUS(stat)) |
|
447 | if (WEXITSTATUS(stat)) |
|
452 | - fprintf(stderr,"Exited with error status %d.", WEXITSTATUS(stat)); |
|
448 | + fprintf(stderr,"Exited with error status %d.\n", WEXITSTATUS(stat)); |
|
453 | else if ((use_wall_clock ? wall : total.tv_sec) > timeout) |
|
449 | else if ((use_wall_clock ? wall : total.tv_sec) > timeout) |
|
454 | - fprintf(stderr,"Time limit exceeded."); |
|
450 | + fprintf(stderr,"Time limit exceeded.\n"); |
|
455 | else |
|
451 | else |
|
456 | // report OK and statistics |
|
452 | // report OK and statistics |
|
457 | fprintf(stderr,"OK\n"); |
|
453 | fprintf(stderr,"OK\n"); |
|
458 |
|
454 | ||
|
459 | fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n", |
|
455 | fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n", |
|
460 | wall, |
|
456 | wall, |
|
461 | (double) rus.ru_utime.tv_sec + |
|
457 | (double) rus.ru_utime.tv_sec + |
|
462 | ((double) rus.ru_utime.tv_usec/1000000.0), |
|
458 | ((double) rus.ru_utime.tv_usec/1000000.0), |
|
463 | (double) rus.ru_stime.tv_sec + |
|
459 | (double) rus.ru_stime.tv_sec + |
|
464 | ((double) rus.ru_stime.tv_usec/1000000.0), |
|
460 | ((double) rus.ru_stime.tv_usec/1000000.0), |
|
465 | max_mem_used); |
|
461 | max_mem_used); |
|
466 | /* |
|
462 | /* |
|
467 | (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n", |
|
463 | (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n", |
|
468 | (double) total.tv_sec + ((double)total.tv_usec / 1000000.0), |
|
464 | (double) total.tv_sec + ((double)total.tv_usec / 1000000.0), |
|
469 | (int) total.tv_usec, |
|
465 | (int) total.tv_usec, |
|
470 | wall, |
|
466 | wall, |
|
471 | syscall_count, |
|
467 | syscall_count, |
|
472 | max_mem_used); |
|
468 | max_mem_used); |
|
473 | */ |
|
469 | */ |
|
474 | exit(0); |
|
470 | exit(0); |
|
475 | } |
|
471 | } |
|
476 | if (WIFSIGNALED(stat)) |
|
472 | if (WIFSIGNALED(stat)) |
|
477 | { |
|
473 | { |
|
478 | box_pid = 0; |
|
474 | box_pid = 0; |
|
479 | - fprintf(stderr,"Caught fatal signal %d.", WTERMSIG(stat)); |
|
475 | + fprintf(stderr,"Caught fatal signal %d.\n", WTERMSIG(stat)); |
|
480 |
|
476 | ||
|
481 | struct timeval total; |
|
477 | struct timeval total; |
|
482 | int wall; |
|
478 | int wall; |
|
483 | wall = time(NULL) - start_time; |
|
479 | wall = time(NULL) - start_time; |
|
484 | timeradd(&rus.ru_utime, &rus.ru_stime, &total); |
|
480 | timeradd(&rus.ru_utime, &rus.ru_stime, &total); |
|
485 | fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n", |
|
481 | fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n", |
|
486 | wall, |
|
482 | wall, |
|
487 | (double) rus.ru_utime.tv_sec + |
|
483 | (double) rus.ru_utime.tv_sec + |
|
488 | ((double) rus.ru_utime.tv_usec/1000000.0), |
|
484 | ((double) rus.ru_utime.tv_usec/1000000.0), |
|
489 | (double) rus.ru_stime.tv_sec + |
|
485 | (double) rus.ru_stime.tv_sec + |
|
490 | ((double) rus.ru_stime.tv_usec/1000000.0), |
|
486 | ((double) rus.ru_stime.tv_usec/1000000.0), |
|
491 | max_mem_used); |
|
487 | max_mem_used); |
|
492 | exit(0); |
|
488 | exit(0); |
|
493 | } |
|
489 | } |
|
494 | if (WIFSTOPPED(stat)) |
|
490 | if (WIFSTOPPED(stat)) |
|
495 | { |
|
491 | { |
|
496 | int sig = WSTOPSIG(stat); |
|
492 | int sig = WSTOPSIG(stat); |
|
497 | if (sig == SIGTRAP) |
|
493 | if (sig == SIGTRAP) |
|
498 | { |
|
494 | { |
|
499 | struct user u; |
|
495 | struct user u; |
|
500 | static int stop_count = -1; |
|
496 | static int stop_count = -1; |
|
501 | if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0) |
|
497 | if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0) |
|
502 | die("ptrace(PTRACE_GETREGS): %m"); |
|
498 | die("ptrace(PTRACE_GETREGS): %m"); |
|
503 | stop_count++; |
|
499 | stop_count++; |
|
504 | if (!stop_count) /* Traceme request */ |
|
500 | if (!stop_count) /* Traceme request */ |
|
505 | log(">> Traceme request caught\n"); |
|
501 | log(">> Traceme request caught\n"); |
|
506 | else if (stop_count & 1) /* Syscall entry */ |
|
502 | else if (stop_count & 1) /* Syscall entry */ |
|
507 | { |
|
503 | { |
|
508 | log(">> Syscall %3ld (%08lx,%08lx,%08lx) ", u.regs.orig_eax, u.regs.ebx, u.regs.ecx, u.regs.edx); |
|
504 | log(">> Syscall %3ld (%08lx,%08lx,%08lx) ", u.regs.orig_eax, u.regs.ebx, u.regs.ecx, u.regs.edx); |
|
509 | syscall_count++; |
|
505 | syscall_count++; |
|
510 | if (!valid_syscall(&u)) |
|
506 | if (!valid_syscall(&u)) |
|
511 | { |
|
507 | { |
|
512 | /* |
|
508 | /* |
|
513 | * Unfortunately, PTRACE_KILL kills _after_ the syscall completes, |
|
509 | * Unfortunately, PTRACE_KILL kills _after_ the syscall completes, |
|
514 | * so we have to change it to something harmless (e.g., an undefined |
|
510 | * so we have to change it to something harmless (e.g., an undefined |
|
515 | * syscall) and make the program continue. |
|
511 | * syscall) and make the program continue. |
|
516 | */ |
|
512 | */ |
|
517 | unsigned int sys = u.regs.orig_eax; |
|
513 | unsigned int sys = u.regs.orig_eax; |
|
518 | u.regs.orig_eax = 0xffffffff; |
|
514 | u.regs.orig_eax = 0xffffffff; |
|
519 | if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0) |
|
515 | if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0) |
|
520 | die("ptrace(PTRACE_SETREGS): %m"); |
|
516 | die("ptrace(PTRACE_SETREGS): %m"); |
|
521 | die("Forbidden syscall %d.", sys); |
|
517 | die("Forbidden syscall %d.", sys); |
|
522 | } |
|
518 | } |
|
523 | } |
|
519 | } |
|
524 | else /* Syscall return */ |
|
520 | else /* Syscall return */ |
|
525 | log("= %ld\n", u.regs.eax); |
|
521 | log("= %ld\n", u.regs.eax); |
|
526 | ptrace(PTRACE_SYSCALL, box_pid, 0, 0); |
|
522 | ptrace(PTRACE_SYSCALL, box_pid, 0, 0); |
|
527 | } |
|
523 | } |
|
528 | else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ) |
|
524 | else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ) |
|
529 | { |
|
525 | { |
|
530 | log(">> Signal %d\n", sig); |
|
526 | log(">> Signal %d\n", sig); |
|
531 | ptrace(PTRACE_SYSCALL, box_pid, 0, sig); |
|
527 | ptrace(PTRACE_SYSCALL, box_pid, 0, sig); |
|
532 | } |
|
528 | } |
|
533 | else |
|
529 | else |
|
534 | die("Received signal %d.", sig); |
|
530 | die("Received signal %d.", sig); |
|
535 | } |
|
531 | } |
|
536 | else |
|
532 | else |
|
537 | die("wait4: unknown status %x, giving up!", stat); |
|
533 | die("wait4: unknown status %x, giving up!", stat); |
|
538 | } |
|
534 | } |
|
539 | } |
|
535 | } |
|
540 |
|
536 | ||
|
541 | static void |
|
537 | static void |
|
542 | box_inside(int argc, char **argv) |
|
538 | box_inside(int argc, char **argv) |
|
543 | { |
|
539 | { |
|
544 | struct rlimit rl; |
|
540 | struct rlimit rl; |
|
545 | char *args[argc+1]; |
|
541 | char *args[argc+1]; |
|
546 | char *env[1] = { NULL }; |
|
542 | char *env[1] = { NULL }; |
|
547 |
|
543 | ||
|
548 | memcpy(args, argv, argc * sizeof(char *)); |
|
544 | memcpy(args, argv, argc * sizeof(char *)); |
|
549 | args[argc] = NULL; |
|
545 | args[argc] = NULL; |
|
550 | if (set_cwd && chdir(set_cwd)) |
|
546 | if (set_cwd && chdir(set_cwd)) |
|
551 | die("chdir: %m"); |
|
547 | die("chdir: %m"); |
|
552 | if (redir_stdin) |
|
548 | if (redir_stdin) |
|
553 | { |
|
549 | { |
|
554 | close(0); |
|
550 | close(0); |
|
555 | if (open(redir_stdin, O_RDONLY) != 0) |
|
551 | if (open(redir_stdin, O_RDONLY) != 0) |
|
556 | die("open(\"%s\"): %m", redir_stdin); |
|
552 | die("open(\"%s\"): %m", redir_stdin); |
|
557 | } |
|
553 | } |
|
558 | if (redir_stdout) |
|
554 | if (redir_stdout) |
|
559 | { |
|
555 | { |
|
560 | close(1); |
|
556 | close(1); |
|
561 | if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1) |
|
557 | if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1) |
|
562 | die("open(\"%s\"): %m", redir_stdout); |
|
558 | die("open(\"%s\"): %m", redir_stdout); |
|
563 | } |
|
559 | } |
|
564 | dup2(1, 2); |
|
560 | dup2(1, 2); |
|
565 | setpgrp(); |
|
561 | setpgrp(); |
|
566 | if (memory_limit) |
|
562 | if (memory_limit) |
|
567 | { |
|
563 | { |
|
568 | rl.rlim_cur = rl.rlim_max = memory_limit * 1024; |
|
564 | rl.rlim_cur = rl.rlim_max = memory_limit * 1024; |
|
569 | if (setrlimit(RLIMIT_AS, &rl) < 0) |
|
565 | if (setrlimit(RLIMIT_AS, &rl) < 0) |
|
570 | die("setrlimit: %m"); |
|
566 | die("setrlimit: %m"); |
|
571 | } |
|
567 | } |
|
572 | rl.rlim_cur = rl.rlim_max = 64; |
|
568 | rl.rlim_cur = rl.rlim_max = 64; |
|
573 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) |
|
569 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) |
|
574 | die("setrlimit: %m"); |
|
570 | die("setrlimit: %m"); |
|
575 | if (filter_syscalls && ptrace(PTRACE_TRACEME) < 0) |
|
571 | if (filter_syscalls && ptrace(PTRACE_TRACEME) < 0) |
@@ -1,23 +1,41 | |||||
|
1 | #include <stdio.h> |
|
1 | #include <stdio.h> |
|
2 | #include <stdlib.h> |
|
2 | #include <stdlib.h> |
|
3 | #include <unistd.h> |
|
3 | #include <unistd.h> |
|
|
4 | + #include <sys/time.h> | ||
|
|
5 | + #include <time.h> | ||
|
|
6 | + #include <sys/resource.h> | ||
|
4 |
|
7 | ||
|
5 | int main() |
|
8 | int main() |
|
6 | { |
|
9 | { |
|
7 | int a,b; |
|
10 | int a,b; |
|
8 |
|
11 | ||
|
9 | int c=0; |
|
12 | int c=0; |
|
10 |
|
13 | ||
|
11 | scanf("%d %d",&a,&b); |
|
14 | scanf("%d %d",&a,&b); |
|
12 | printf("%d\n",a+b); |
|
15 | printf("%d\n",a+b); |
|
13 |
|
16 | ||
|
14 | - sleep(1); |
|
17 | + struct rusage ru; |
|
15 |
|
18 | ||
|
16 | - c = 0; |
|
19 | + while(1) { |
|
17 | - while(c<1000000000) { |
|
20 | + c++; |
|
|
21 | + b+=c; | ||
|
|
22 | + while(c<1000000000) { | ||
|
|
23 | + c++; | ||
|
|
24 | + b+=c; | ||
|
|
25 | + } | ||
|
|
26 | + getrusage(RUSAGE_SELF,&ru); | ||
|
|
27 | + if((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec)>=1) | ||
|
|
28 | + break; | ||
|
|
29 | + } | ||
|
|
30 | + printf("%d\n",b); | ||
|
|
31 | + c=0; | ||
|
|
32 | + while(c<100000000) { | ||
|
18 | c++; |
|
33 | c++; |
|
19 | b+=c; |
|
34 | b+=c; |
|
20 | } |
|
35 | } |
|
|
36 | + if(b==10) | ||
|
|
37 | + printf("hello\n"); | ||
|
|
38 | + | ||
|
21 | exit(0); |
|
39 | exit(0); |
|
22 | } |
|
40 | } |
|
23 |
|
41 |
You need to be logged in to leave comments.
Login now