system.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* cpufreq-bench CPUFreq microbenchmark
  2. *
  3. * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #include <sched.h>
  25. #include <cpufreq.h>
  26. #include <cpupower.h>
  27. #include "config.h"
  28. #include "system.h"
  29. /**
  30. * returns time since epoch in µs
  31. *
  32. * @retval time
  33. **/
  34. long long int get_time()
  35. {
  36. struct timeval now;
  37. gettimeofday(&now, NULL);
  38. return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
  39. }
  40. /**
  41. * sets the cpufreq governor
  42. *
  43. * @param governor cpufreq governor name
  44. * @param cpu cpu for which the governor should be set
  45. *
  46. * @retval 0 on success
  47. * @retval -1 when failed
  48. **/
  49. int set_cpufreq_governor(char *governor, unsigned int cpu)
  50. {
  51. dprintf("set %s as cpufreq governor\n", governor);
  52. if (cpupower_is_cpu_online(cpu) != 1) {
  53. perror("cpufreq_cpu_exists");
  54. fprintf(stderr, "error: cpu %u does not exist\n", cpu);
  55. return -1;
  56. }
  57. if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
  58. perror("cpufreq_modify_policy_governor");
  59. fprintf(stderr, "error: unable to set %s governor\n", governor);
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. /**
  65. * sets cpu affinity for the process
  66. *
  67. * @param cpu cpu# to which the affinity should be set
  68. *
  69. * @retval 0 on success
  70. * @retval -1 when setting the affinity failed
  71. **/
  72. int set_cpu_affinity(unsigned int cpu)
  73. {
  74. cpu_set_t cpuset;
  75. CPU_ZERO(&cpuset);
  76. CPU_SET(cpu, &cpuset);
  77. dprintf("set affinity to cpu #%u\n", cpu);
  78. if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
  79. perror("sched_setaffinity");
  80. fprintf(stderr, "warning: unable to set cpu affinity\n");
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. /**
  86. * sets the process priority parameter
  87. *
  88. * @param priority priority value
  89. *
  90. * @retval 0 on success
  91. * @retval -1 when setting the priority failed
  92. **/
  93. int set_process_priority(int priority)
  94. {
  95. struct sched_param param;
  96. dprintf("set scheduler priority to %i\n", priority);
  97. param.sched_priority = priority;
  98. if (sched_setscheduler(0, SCHEDULER, &param) < 0) {
  99. perror("sched_setscheduler");
  100. fprintf(stderr, "warning: unable to set scheduler priority\n");
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. /**
  106. * notifies the user that the benchmark may run some time
  107. *
  108. * @param config benchmark config values
  109. *
  110. **/
  111. void prepare_user(const struct config *config)
  112. {
  113. unsigned long sleep_time = 0;
  114. unsigned long load_time = 0;
  115. unsigned int round;
  116. for (round = 0; round < config->rounds; round++) {
  117. sleep_time += 2 * config->cycles *
  118. (config->sleep + config->sleep_step * round);
  119. load_time += 2 * config->cycles *
  120. (config->load + config->load_step * round) +
  121. (config->load + config->load_step * round * 4);
  122. }
  123. if (config->verbose || config->output != stdout)
  124. printf("approx. test duration: %im\n",
  125. (int)((sleep_time + load_time) / 60000000));
  126. }
  127. /**
  128. * sets up the cpu affinity and scheduler priority
  129. *
  130. * @param config benchmark config values
  131. *
  132. **/
  133. void prepare_system(const struct config *config)
  134. {
  135. if (config->verbose)
  136. printf("set cpu affinity to cpu #%u\n", config->cpu);
  137. set_cpu_affinity(config->cpu);
  138. switch (config->prio) {
  139. case SCHED_HIGH:
  140. if (config->verbose)
  141. printf("high priority condition requested\n");
  142. set_process_priority(PRIORITY_HIGH);
  143. break;
  144. case SCHED_LOW:
  145. if (config->verbose)
  146. printf("low priority condition requested\n");
  147. set_process_priority(PRIORITY_LOW);
  148. break;
  149. default:
  150. if (config->verbose)
  151. printf("default priority condition requested\n");
  152. set_process_priority(PRIORITY_DEFAULT);
  153. }
  154. }