static gboolean cpufreq_selector_libcpufreq_validate_governor (CPUFreqSelectorLibcpufreq *selector, const gchar *governor) { guint cpu; CPUFreqGovernorList *govs, *gov; g_object_get (G_OBJECT (selector), "cpu", &cpu, NULL); govs = cpufreq_get_available_governors (cpu); if (!govs) return FALSE; for (gov = govs; gov; gov = gov->next) { if (g_ascii_strcasecmp (gov->governor, governor) == 0) { cpufreq_put_available_governors (govs); return TRUE; } } cpufreq_put_available_governors (govs); return FALSE; }
struct cpufreq_available_governors *cpufreq_get_available_governors(unsigned int cpu) { if (cpu >= MAX_CPUS) { errno = ENOENT; return NULL; } struct cpufreq_available_governors *first = NULL; struct cpufreq_available_governors *current = NULL; char* governors[] = { "conservative", "ondemand", "powersave", "userspace", "performance" }; unsigned long i; for(i = 0; i < 5; i++) { if (governors[i] == NULL) { continue; } struct cpufreq_available_governors *next = malloc(sizeof(struct cpufreq_available_governors)); if (!next) { cpufreq_put_available_governors(first); return NULL; } if (current) { current->next = next; } current = next; current->governor = strndup(governors[i], BUFFER_SIZE); if (!current->governor) { cpufreq_put_available_governors(first); return NULL; } if (first == NULL) { first = current; } current->first = first; } return first; }
static void next_governor(unsigned int cpu) { struct cpufreq_available_governors *govs; for ( govs = cpufreq_get_available_governors(cpu); (govs->next!= NULL) && strcmp(govs->governor,governor[cpu]); govs=govs->next ) {} char cmd[length]; sprintf(cmd, "sudo cpupower -c %u frequency-set -g %s", cpu, govs->next==NULL ? govs->first->governor : govs->next->governor); system(cmd); cpufreq_put_available_governors(govs->first); }
JNIEXPORT jobjectArray JNICALL Java_platforms_x86_X86_1DVFS_getAvailableGovernors (JNIEnv *env, jobject obj, jint cpu) { struct cpufreq_available_governors *govs, *first = NULL; jobjectArray govNames; jclass cls; jstring str = NULL; int i = 0, length = 0; govs = cpufreq_get_available_governors((unsigned int)cpu); if (govs) first = govs->first; while (govs) { govs = govs->next; length++; } cls = (*env)->FindClass(env, "Ljava/lang/String;"); govNames = (*env)->NewObjectArray(env, (jsize) length, cls, NULL); if (!first) return govNames; govs = first; while (govs) { str = (*env)->NewStringUTF(env, govs->governor); (*env)->SetObjectArrayElement(env, govNames, i, str); (*env)->DeleteLocalRef(env, str); i++; govs = govs->next; } (*env)->DeleteLocalRef(env, cls); cpufreq_put_available_governors(first); return govNames; }
static GList * cpufreq_monitor_libcpufreq_get_available_governors (CPUFreqMonitor *monitor) { guint cpu; GList *list = NULL; CPUFreqGovernorList *govs, *gov; g_object_get (G_OBJECT (monitor), "cpu", &cpu, NULL); govs = cpufreq_get_available_governors (cpu); if (!govs) return NULL; for (gov = govs; gov; gov = gov->next) { list = g_list_prepend (list, g_strdup (gov->governor)); } cpufreq_put_available_governors (govs); return list; }
void print_cpu(unsigned int cpu, unsigned int n_cpus) { int i; char str[MAXLEN]; char premark[MAXLEN]; char postmark[MAXLEN]; unsigned long curfreq, freq; struct cpufreq_available_frequencies* freqs; struct cpufreq_available_governors* govs; struct cpufreq_policy* policy; if (cpu + 1 == n_cpus) // last cpu column printf("<item label='cpu %u'/>\n", cpu); else if (cpu + 1 < n_cpus) { printf("<menu label='cpu %u' id='obcpufreq-cpu%u'>\n", cpu, cpu + 1); print_cpu(cpu + 1, n_cpus); printf("</menu>\n"); } printf(SEPARATOR); curfreq = cpufreq_get_freq_kernel(cpu); policy = cpufreq_get_policy(cpu); // print available governors govs = cpufreq_get_available_governors(cpu); if (govs) { printf("<menu label='governors' id='obcpufreq-governors%u'>\n", cpu); for (; govs; govs = govs->next) { premark[0] = ' '; premark[1] = '\0'; if (!strcmp(govs->governor, policy->governor)) premark[0] = '*'; printf(" <item label='%s%s'><action name='Execute'><command>" CPUFREQ_SET " --cpu %u -g %s</command></action></item>\n", premark, govs->governor, cpu, govs->governor); } printf("</menu>\n"); cpufreq_put_available_governors(govs); } else printf("<item label='%s'/>\n", "no available governors"); printf(SEPARATOR); // print available freqs freqs = cpufreq_get_available_frequencies(cpu); if (freqs) { for (i = 0; freqs; freqs = freqs->next, i++) { freq = freqs->frequency; get_human_speed(str, freq); premark[0] = ' '; premark[1] = '\0'; if (freq == curfreq) premark[0] = '*'; postmark[0] = '\0'; if (policy) { if (freq == policy->max && freq == policy->min) strcpy(postmark, " (max,min)"); else if (freq == policy->max) strcpy(postmark, " (max)"); else if (freq == policy->min) strcpy(postmark, " (min)"); } printf("<menu label='%s%s%s' id='obcpufreq-freq%d'>\n" " <item label='set as max'><action name='Execute'><command>" CPUFREQ_SET " --cpu %u --max %lu</command></action></item>\n" " <item label='set as min'><action name='Execute'><command>" CPUFREQ_SET " --cpu %u --min %lu</command></action></item>\n</menu>\n", premark, str, postmark, i, cpu, freqs->frequency, cpu, freqs->frequency); } cpufreq_put_available_frequencies(freqs); } else printf("<item label='%s'/>\n", "no available freqs"); cpufreq_put_policy(policy); return; }