system.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "config.h"
  27. #include "system.h"
  28. /**
  29. * returns time since epoch in µs
  30. *
  31. * @retval time
  32. **/
  33. long long int get_time()
  34. {
  35. struct timeval now;
  36. gettimeofday(&now, NULL);
  37. return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
  38. }
  39. /**
  40. * sets the cpufreq governor
  41. *
  42. * @param governor cpufreq governor name
  43. * @param cpu cpu for which the governor should be set
  44. *
  45. * @retval 0 on success
  46. * @retval -1 when failed
  47. **/
  48. int set_cpufreq_governor(char *governor, unsigned int cpu)
  49. {
  50. dprintf("set %s as cpufreq governor\n", governor);
  51. if (cpufreq_cpu_exists(cpu) != 0) {
  52. perror("cpufreq_cpu_exists");
  53. fprintf(stderr, "error: cpu %u does not exist\n", cpu);
  54. return -1;
  55. }
  56. if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
  57. perror("cpufreq_modify_policy_governor");
  58. fprintf(stderr, "error: unable to set %s governor\n", governor);
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. /**
  64. * sets cpu affinity for the process
  65. *
  66. * @param cpu cpu# to which the affinity should be set
  67. *
  68. * @retval 0 on success
  69. * @retval -1 when setting the affinity failed
  70. **/
  71. int set_cpu_affinity(unsigned int cpu)
  72. {
  73. cpu_set_t cpuset;
  74. CPU_ZERO(&cpuset);
  75. CPU_SET(cpu, &cpuset);
  76. dprintf("set affinity to cpu #%u\n", cpu);
  77. if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
  78. perror("sched_setaffinity");
  79. fprintf(stderr, "warning: unable to set cpu affinity\n");
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. /**
  85. * sets the process priority parameter
  86. *
  87. * @param priority priority value
  88. *
  89. * @retval 0 on success
  90. * @retval -1 when setting the priority failed
  91. **/
  92. int set_process_priority(int priority)
  93. {
  94. struct sched_param param;
  95. dprintf("set scheduler priority to %i\n", priority);
  96. param.sched_priority = priority;
  97. if (sched_setscheduler(0, SCHEDULER, &param) < 0) {
  98. perror("sched_setscheduler");
  99. fprintf(stderr, "warning: unable to set scheduler priority\n");
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * notifies the user that the benchmark may run some time
  106. *
  107. * @param config benchmark config values
  108. *
  109. **/
  110. void prepare_user(const struct config *config)
  111. {
  112. unsigned long sleep_time = 0;
  113. unsigned long load_time = 0;
  114. unsigned int round;
  115. for (round = 0; round < config->rounds; round++) {
  116. sleep_time += 2 * config->cycles *
  117. (config->sleep + config->sleep_step * round);
  118. load_time += 2 * config->cycles *
  119. (config->load + config->load_step * round) +
  120. (config->load + config->load_step * round * 4);
  121. }
  122. if (config->verbose || config->output != stdout)
  123. printf("approx. test duration: %im\n",
  124. (int)((sleep_time + load_time) / 60000000));
  125. }
  126. /**
  127. * sets up the cpu affinity and scheduler priority
  128. *
  129. * @param config benchmark config values
  130. *
  131. **/
  132. void prepare_system(const struct config *config)
  133. {
  134. if (config->verbose)
  135. printf("set cpu affinity to cpu #%u\n", config->cpu);
  136. set_cpu_affinity(config->cpu);
  137. switch (config->prio) {
  138. case SCHED_HIGH:
  139. if (config->verbose)
  140. printf("high priority condition requested\n");
  141. set_process_priority(PRIORITY_HIGH);
  142. break;
  143. case SCHED_LOW:
  144. if (config->verbose)
  145. printf("low priority condition requested\n");
  146. set_process_priority(PRIORITY_LOW);
  147. break;
  148. default:
  149. if (config->verbose)
  150. printf("default priority condition requested\n");
  151. set_process_priority(PRIORITY_DEFAULT);
  152. }
  153. }