cpufreq_stats.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * drivers/cpufreq/cpufreq_stats.c
  3. *
  4. * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  5. * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/cpu.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/cputime.h>
  16. static DEFINE_SPINLOCK(cpufreq_stats_lock);
  17. struct cpufreq_stats {
  18. unsigned int total_trans;
  19. unsigned long long last_time;
  20. unsigned int max_state;
  21. unsigned int state_num;
  22. unsigned int last_index;
  23. u64 *time_in_state;
  24. unsigned int *freq_table;
  25. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  26. unsigned int *trans_table;
  27. #endif
  28. };
  29. static int cpufreq_stats_update(struct cpufreq_stats *stats)
  30. {
  31. unsigned long long cur_time = get_jiffies_64();
  32. spin_lock(&cpufreq_stats_lock);
  33. stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
  34. stats->last_time = cur_time;
  35. spin_unlock(&cpufreq_stats_lock);
  36. return 0;
  37. }
  38. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  39. {
  40. return sprintf(buf, "%d\n", policy->stats->total_trans);
  41. }
  42. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  43. {
  44. struct cpufreq_stats *stats = policy->stats;
  45. ssize_t len = 0;
  46. int i;
  47. if (policy->fast_switch_enabled)
  48. return 0;
  49. cpufreq_stats_update(stats);
  50. for (i = 0; i < stats->state_num; i++) {
  51. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  52. (unsigned long long)
  53. jiffies_64_to_clock_t(stats->time_in_state[i]));
  54. }
  55. return len;
  56. }
  57. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  58. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  59. {
  60. struct cpufreq_stats *stats = policy->stats;
  61. ssize_t len = 0;
  62. int i, j;
  63. if (policy->fast_switch_enabled)
  64. return 0;
  65. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  66. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  67. for (i = 0; i < stats->state_num; i++) {
  68. if (len >= PAGE_SIZE)
  69. break;
  70. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  71. stats->freq_table[i]);
  72. }
  73. if (len >= PAGE_SIZE)
  74. return PAGE_SIZE;
  75. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  76. for (i = 0; i < stats->state_num; i++) {
  77. if (len >= PAGE_SIZE)
  78. break;
  79. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  80. stats->freq_table[i]);
  81. for (j = 0; j < stats->state_num; j++) {
  82. if (len >= PAGE_SIZE)
  83. break;
  84. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  85. stats->trans_table[i*stats->max_state+j]);
  86. }
  87. if (len >= PAGE_SIZE)
  88. break;
  89. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  90. }
  91. if (len >= PAGE_SIZE)
  92. return PAGE_SIZE;
  93. return len;
  94. }
  95. cpufreq_freq_attr_ro(trans_table);
  96. #endif
  97. cpufreq_freq_attr_ro(total_trans);
  98. cpufreq_freq_attr_ro(time_in_state);
  99. static struct attribute *default_attrs[] = {
  100. &total_trans.attr,
  101. &time_in_state.attr,
  102. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  103. &trans_table.attr,
  104. #endif
  105. NULL
  106. };
  107. static struct attribute_group stats_attr_group = {
  108. .attrs = default_attrs,
  109. .name = "stats"
  110. };
  111. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  112. {
  113. int index;
  114. for (index = 0; index < stats->max_state; index++)
  115. if (stats->freq_table[index] == freq)
  116. return index;
  117. return -1;
  118. }
  119. void cpufreq_stats_free_table(struct cpufreq_policy *policy)
  120. {
  121. struct cpufreq_stats *stats = policy->stats;
  122. /* Already freed */
  123. if (!stats)
  124. return;
  125. pr_debug("%s: Free stats table\n", __func__);
  126. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  127. kfree(stats->time_in_state);
  128. kfree(stats);
  129. policy->stats = NULL;
  130. }
  131. void cpufreq_stats_create_table(struct cpufreq_policy *policy)
  132. {
  133. unsigned int i = 0, count = 0, ret = -ENOMEM;
  134. struct cpufreq_stats *stats;
  135. unsigned int alloc_size;
  136. struct cpufreq_frequency_table *pos, *table;
  137. /* We need cpufreq table for creating stats table */
  138. table = policy->freq_table;
  139. if (unlikely(!table))
  140. return;
  141. /* stats already initialized */
  142. if (policy->stats)
  143. return;
  144. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  145. if (!stats)
  146. return;
  147. /* Find total allocation size */
  148. cpufreq_for_each_valid_entry(pos, table)
  149. count++;
  150. alloc_size = count * sizeof(int) + count * sizeof(u64);
  151. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  152. alloc_size += count * count * sizeof(int);
  153. #endif
  154. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  155. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  156. if (!stats->time_in_state)
  157. goto free_stat;
  158. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  159. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  160. stats->trans_table = stats->freq_table + count;
  161. #endif
  162. stats->max_state = count;
  163. /* Find valid-unique entries */
  164. cpufreq_for_each_valid_entry(pos, table)
  165. if (freq_table_get_index(stats, pos->frequency) == -1)
  166. stats->freq_table[i++] = pos->frequency;
  167. stats->state_num = i;
  168. stats->last_time = get_jiffies_64();
  169. stats->last_index = freq_table_get_index(stats, policy->cur);
  170. policy->stats = stats;
  171. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  172. if (!ret)
  173. return;
  174. /* We failed, release resources */
  175. policy->stats = NULL;
  176. kfree(stats->time_in_state);
  177. free_stat:
  178. kfree(stats);
  179. }
  180. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  181. unsigned int new_freq)
  182. {
  183. struct cpufreq_stats *stats = policy->stats;
  184. int old_index, new_index;
  185. if (!stats) {
  186. pr_debug("%s: No stats found\n", __func__);
  187. return;
  188. }
  189. old_index = stats->last_index;
  190. new_index = freq_table_get_index(stats, new_freq);
  191. /* We can't do stats->time_in_state[-1]= .. */
  192. if (old_index == -1 || new_index == -1 || old_index == new_index)
  193. return;
  194. cpufreq_stats_update(stats);
  195. stats->last_index = new_index;
  196. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  197. stats->trans_table[old_index * stats->max_state + new_index]++;
  198. #endif
  199. stats->total_trans++;
  200. }