cpufreq_stats.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. static DEFINE_SPINLOCK(cpufreq_stats_lock);
  16. struct cpufreq_stats {
  17. unsigned int total_trans;
  18. unsigned long long last_time;
  19. unsigned int max_state;
  20. unsigned int state_num;
  21. unsigned int last_index;
  22. u64 *time_in_state;
  23. unsigned int *freq_table;
  24. unsigned int *trans_table;
  25. };
  26. static int cpufreq_stats_update(struct cpufreq_stats *stats)
  27. {
  28. unsigned long long cur_time = get_jiffies_64();
  29. spin_lock(&cpufreq_stats_lock);
  30. stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
  31. stats->last_time = cur_time;
  32. spin_unlock(&cpufreq_stats_lock);
  33. return 0;
  34. }
  35. static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
  36. {
  37. unsigned int count = stats->max_state;
  38. memset(stats->time_in_state, 0, count * sizeof(u64));
  39. memset(stats->trans_table, 0, count * count * sizeof(int));
  40. stats->last_time = get_jiffies_64();
  41. stats->total_trans = 0;
  42. }
  43. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  44. {
  45. return sprintf(buf, "%d\n", policy->stats->total_trans);
  46. }
  47. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  48. {
  49. struct cpufreq_stats *stats = policy->stats;
  50. ssize_t len = 0;
  51. int i;
  52. if (policy->fast_switch_enabled)
  53. return 0;
  54. cpufreq_stats_update(stats);
  55. for (i = 0; i < stats->state_num; i++) {
  56. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  57. (unsigned long long)
  58. jiffies_64_to_clock_t(stats->time_in_state[i]));
  59. }
  60. return len;
  61. }
  62. static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
  63. size_t count)
  64. {
  65. /* We don't care what is written to the attribute. */
  66. cpufreq_stats_clear_table(policy->stats);
  67. return count;
  68. }
  69. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  70. {
  71. struct cpufreq_stats *stats = policy->stats;
  72. ssize_t len = 0;
  73. int i, j;
  74. if (policy->fast_switch_enabled)
  75. return 0;
  76. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  77. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  78. for (i = 0; i < stats->state_num; i++) {
  79. if (len >= PAGE_SIZE)
  80. break;
  81. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  82. stats->freq_table[i]);
  83. }
  84. if (len >= PAGE_SIZE)
  85. return PAGE_SIZE;
  86. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  87. for (i = 0; i < stats->state_num; i++) {
  88. if (len >= PAGE_SIZE)
  89. break;
  90. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  91. stats->freq_table[i]);
  92. for (j = 0; j < stats->state_num; j++) {
  93. if (len >= PAGE_SIZE)
  94. break;
  95. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  96. stats->trans_table[i*stats->max_state+j]);
  97. }
  98. if (len >= PAGE_SIZE)
  99. break;
  100. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  101. }
  102. if (len >= PAGE_SIZE)
  103. return PAGE_SIZE;
  104. return len;
  105. }
  106. cpufreq_freq_attr_ro(trans_table);
  107. cpufreq_freq_attr_ro(total_trans);
  108. cpufreq_freq_attr_ro(time_in_state);
  109. cpufreq_freq_attr_wo(reset);
  110. static struct attribute *default_attrs[] = {
  111. &total_trans.attr,
  112. &time_in_state.attr,
  113. &reset.attr,
  114. &trans_table.attr,
  115. NULL
  116. };
  117. static const struct attribute_group stats_attr_group = {
  118. .attrs = default_attrs,
  119. .name = "stats"
  120. };
  121. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  122. {
  123. int index;
  124. for (index = 0; index < stats->max_state; index++)
  125. if (stats->freq_table[index] == freq)
  126. return index;
  127. return -1;
  128. }
  129. void cpufreq_stats_free_table(struct cpufreq_policy *policy)
  130. {
  131. struct cpufreq_stats *stats = policy->stats;
  132. /* Already freed */
  133. if (!stats)
  134. return;
  135. pr_debug("%s: Free stats table\n", __func__);
  136. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  137. kfree(stats->time_in_state);
  138. kfree(stats);
  139. policy->stats = NULL;
  140. }
  141. void cpufreq_stats_create_table(struct cpufreq_policy *policy)
  142. {
  143. unsigned int i = 0, count = 0, ret = -ENOMEM;
  144. struct cpufreq_stats *stats;
  145. unsigned int alloc_size;
  146. struct cpufreq_frequency_table *pos;
  147. count = cpufreq_table_count_valid_entries(policy);
  148. if (!count)
  149. return;
  150. /* stats already initialized */
  151. if (policy->stats)
  152. return;
  153. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  154. if (!stats)
  155. return;
  156. alloc_size = count * sizeof(int) + count * sizeof(u64);
  157. alloc_size += count * count * sizeof(int);
  158. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  159. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  160. if (!stats->time_in_state)
  161. goto free_stat;
  162. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  163. stats->trans_table = stats->freq_table + count;
  164. stats->max_state = count;
  165. /* Find valid-unique entries */
  166. cpufreq_for_each_valid_entry(pos, policy->freq_table)
  167. if (freq_table_get_index(stats, pos->frequency) == -1)
  168. stats->freq_table[i++] = pos->frequency;
  169. stats->state_num = i;
  170. stats->last_time = get_jiffies_64();
  171. stats->last_index = freq_table_get_index(stats, policy->cur);
  172. policy->stats = stats;
  173. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  174. if (!ret)
  175. return;
  176. /* We failed, release resources */
  177. policy->stats = NULL;
  178. kfree(stats->time_in_state);
  179. free_stat:
  180. kfree(stats);
  181. }
  182. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  183. unsigned int new_freq)
  184. {
  185. struct cpufreq_stats *stats = policy->stats;
  186. int old_index, new_index;
  187. if (!stats) {
  188. pr_debug("%s: No stats found\n", __func__);
  189. return;
  190. }
  191. old_index = stats->last_index;
  192. new_index = freq_table_get_index(stats, new_freq);
  193. /* We can't do stats->time_in_state[-1]= .. */
  194. if (old_index == -1 || new_index == -1 || old_index == new_index)
  195. return;
  196. cpufreq_stats_update(stats);
  197. stats->last_index = new_index;
  198. stats->trans_table[old_index * stats->max_state + new_index]++;
  199. stats->total_trans++;
  200. }