static guint 
cpufreq_selector_libcpufreq_get_valid_frequency (CPUFreqSelectorLibcpufreq *selector,
						 guint                      frequency)
{
	guint                 cpu;
	gint                  dist = G_MAXINT;
	guint                 retval = 0;
	CPUFreqFrequencyList *freqs, *freq;

	g_object_get (G_OBJECT (selector),
		      "cpu", &cpu,
		      NULL);

	freqs = cpufreq_get_available_frequencies (cpu);
	if (!freqs)
		return 0;

	for (freq = freqs; freq; freq = freq->next) {
		guint current_dist;
		
		if (freq->frequency == frequency) {
			cpufreq_put_available_frequencies (freqs);

			return frequency;
		}

		current_dist = abs (freq->frequency - frequency);
		if (current_dist < dist) {
			dist = current_dist;
			retval = freq->frequency;
		}
	}

	return retval;
}
static GList *
cpufreq_monitor_libcpufreq_get_available_frequencies (CPUFreqMonitor *monitor)
{
    GList                *list = NULL;
    guint                 cpu;
    CPUFreqFrequencyList *freqs, *freq;

    g_object_get (G_OBJECT (monitor),
                  "cpu", &cpu, NULL);

    freqs = cpufreq_get_available_frequencies (cpu);
    if (!freqs)
        return NULL;

    for (freq = freqs; freq; freq = freq->next) {
        gchar *frequency;

        frequency = g_strdup_printf ("%lu", freq->frequency);

        if (!g_list_find_custom (list, frequency, compare))
            list = g_list_prepend (list, frequency);
        else
            g_free (frequency);
    }

    cpufreq_put_available_frequencies (freqs);

    return g_list_sort (list, compare);
}
Esempio n. 3
0
JNIEXPORT jlongArray JNICALL Java_platforms_x86_X86_1DVFS_getAvailableFrequencies
  (JNIEnv *env, jobject obj, jint cpu)
{
	struct cpufreq_available_frequencies *available_freqs, *first = NULL;
	jlongArray freqs;
	jlong c_freqs[16];
	int i = 0, length = 0;
	jclass cls;
	jfieldID fid;

	available_freqs = cpufreq_get_available_frequencies((unsigned int)cpu);

	if (available_freqs)
		first = available_freqs->first;

	/* First grab the number of available frequencies */
	while (available_freqs) {
		length++;
		available_freqs = available_freqs->next;
	}

	freqs = (*env)->NewLongArray(env, (jsize) length);

	if (!first) {
		cls = (*env)->GetObjectClass(env, obj);
		fid = (*env)->GetFieldID(env, cls, "minFreq", "J");
		(*env)->SetLongField(env, obj, fid, 1);
		(*env)->DeleteLocalRef(env, cls);
		return freqs;
	}

	available_freqs = first;

	while (available_freqs) {
		c_freqs[i] = (jlong)available_freqs->frequency;
		i++;
		available_freqs = available_freqs->next;
	}

	// move from the temp structure to the java structure
	(*env)->SetLongArrayRegion(env, freqs, 0, length, c_freqs);

	/* Free local references */
	cpufreq_put_available_frequencies(first);

	return freqs;
}
Esempio n. 4
0
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;
}