main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* A program to make CPU Benchmark and Stress.
  2. * Copyright (C) 2022
  3. * Alimiracle <alimiracle@riseup.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include <ctype.h>
  19. #include <signal.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <time.h>
  25. #include <sys/wait.h>
  26. #include <sys/time.h>
  27. #include "cmdline.h"
  28. static char * global_progname = "dokimi";
  29. #define out fprintf(stdout, "%s: info: ", global_progname), \
  30. fprintf
  31. #define err fprintf(stderr, "%s error: ", global_progname), \
  32. fprintf
  33. long long count = 0;
  34. int
  35. timeval_subtract(result, x, y)
  36. struct timeval *
  37. result, * x, * y;
  38. {
  39. /* Perform the carry for the later subtraction by updating y. */
  40. if (x -> tv_usec < y -> tv_usec)
  41. {
  42. int nsec = (y -> tv_usec - x -> tv_usec) / 1000000 + 1;
  43. y -> tv_usec -= 1000000 * nsec;
  44. y -> tv_sec += nsec;
  45. }
  46. if (x -> tv_usec - y -> tv_usec > 1000000)
  47. {
  48. int nsec = (x -> tv_usec - y -> tv_usec) / 1000000;
  49. y -> tv_usec += 1000000 * nsec;
  50. y -> tv_sec -= nsec;
  51. }
  52. /* Compute the time remaining to wait.
  53. tv_usec is certainly positive. */
  54. result -> tv_sec = x -> tv_sec - y -> tv_sec;
  55. result -> tv_usec = x -> tv_usec - y -> tv_usec;
  56. /* Return 1 if result is negative. */
  57. return x -> tv_sec < y -> tv_sec;
  58. }
  59. void
  60. test_pid(pid_t pid) {
  61. if (getppid() != pid)
  62. {
  63. exit(0);
  64. }
  65. }
  66. void sig_handler(int signum){
  67. printf("%lld\n", count);
  68. exit(0);
  69. }
  70. int do_task(long long n, long sqrt_n, int time_s) {
  71. if(time_s!=0) {
  72. alarm(time_s);
  73. signal(SIGALRM,sig_handler); // Register signal handler
  74. n=-6;
  75. }
  76. int test_pid_wait = 0;
  77. pid_t pid = getppid();
  78. int i;
  79. double d;
  80. float f;
  81. while (count != n) {
  82. d = sqrt(sqrt_n);
  83. f = sqrt(sqrt_n);
  84. i = sqrt(sqrt_n);
  85. if (test_pid_wait == 100) {
  86. test_pid(pid);
  87. test_pid_wait = 0;
  88. }
  89. test_pid_wait = test_pid_wait + 1;
  90. count = count + 1;
  91. }
  92. return 1;
  93. }
  94. // Come with soft rounded cheeks and eyes as bright As sunlight on a stream;
  95. int main(int argc, char ** argv) {
  96. struct gengetopt_args_info args_info;
  97. if (cmdline_parser(argc, argv, & args_info) != 0)
  98. exit(1);
  99. char * p;
  100. int number_of_workers = args_info.workers_arg;
  101. long number_of_iterations = args_info.iterations_arg;
  102. long number_of_sqrt = args_info.number_sqrt_arg;
  103. int i = 0;
  104. int status = 0;
  105. int time_s=0;
  106. if (number_of_workers == 0) number_of_workers = sysconf(_SC_NPROCESSORS_ONLN);
  107. if (number_of_sqrt == 0) number_of_sqrt = rand();
  108. if(args_info.strikes_flag!=0) time_s = args_info.time_arg;
  109. /* Record our start time. */
  110. unsigned long int elapsed;
  111. struct timeval t_start, t_end, t_diff;
  112. gettimeofday( & t_start, NULL);
  113. fprintf(stdout, "Creating %d children\n", number_of_workers);
  114. fflush(stdout);
  115. for (i = 0; i < number_of_workers; i++) {
  116. pid_t pid = fork();
  117. if (pid == 0) /* only execute this if child */ {
  118. do_task(number_of_iterations, number_of_sqrt, time_s);
  119. exit(0);
  120. }
  121. }
  122. // wait(&status); /* only the parent waits */
  123. while (wait(NULL) > 0);
  124. /* Record our stop time. */
  125. gettimeofday( & t_end, NULL);
  126. timeval_subtract( & t_diff, & t_end, & t_start);
  127. elapsed = (t_diff.tv_sec * 1e6 + t_diff.tv_usec);
  128. /* Print final status message. */
  129. if(args_info.strikes_flag==0) fprintf(stdout, "successful run completed in %lu MS\n",
  130. elapsed);
  131. return 0;
  132. }