int main() { timeval tv; long long begin_usec, end_usec; unsigned long curFreq; // get time before if ( gettimeofday(&tv, NULL) != 0 ) { printf("error while getting time\n"); } begin_usec = tv.tv_sec * 1000000 + tv.tv_usec; // set frequencies int i; for (i=0; i<1; i++) { if (cpufreq_set_frequency(i, 1700000) != 0) { printf("error while setting frequency\n"); } // if (cpufreq_set_frequency(i+20, 1700000) != 0) { // printf("error while setting frequency\n"); // } } // get time after if ( gettimeofday(&tv, NULL) != 0 ) { printf("error while getting time\n"); } end_usec = tv.tv_sec * 1000000 + tv.tv_usec; // print times printf("%lli %lli %lli\n", begin_usec, end_usec, end_usec - begin_usec); return 0; }
void uv_set_cpufreq(void) { int s = 0, cpu = 0, skip = 0; uint64_t cpu_cur = 0; long cpu_togo = 0; char *freq_str = NULL; freq_str = getenv("NODE_CPU0_FREQ"); printf("%s ", freq_str); if (freq_str) { cpu_togo = atol(freq_str); flex_mode = 0; if (cpu_togo == 0) { cpu_togo = 1200000; flex_mode = 1; freq = 0; threshold = atoi(getenv("NODE_QUEUE_THRESHOLD")); printf("FLEX MODE: threshold = %lu\n", threshold); } else if (cpu_togo < 0) { skip = 1; printf("SKIP MODE "); } } else cpu_togo = 3200000; if (!skip) s = cpufreq_set_frequency(cpu, cpu_togo); if (s != 0) { printf("ERROR: uv_set_cpufreq failed!\n"); } cpu_cur = cpufreq_get_freq_kernel(cpu); prog_start = uv__cputime(); printf("Current CPU Frequency: %luKHz\n", cpu_cur); }
int set_process_cpu_and_freq(const unsigned int cpu_index) { // set the core-affinity cpu_set_t cpu_set; CPU_ZERO(&cpu_set); CPU_SET(cpu_index, &cpu_set); sched_setaffinity(0, sizeof(cpu_set), &cpu_set); // set CPU governor unsigned long cpu_max = 0; unsigned long cpu_min = 0; unsigned long cpu_cur = 0; if (cpufreq_cpu_exists(cpu_index) != 0) { printf("Invalid CPU index!\n"); return -1; } if (cpufreq_get_hardware_limits(cpu_index, &cpu_min, &cpu_max) != 0) { printf("Unable to get hardware limits!\n"); return -1; } if (cpufreq_set_frequency(cpu_index, cpu_max) != 0) { printf("Unable to set frequency(%luMHz) Are u root?\n", cpu_max / 1000); return -1; } cpu_cur = (cpufreq_get_freq_kernel(cpu_index) / 1000); printf("Current CPU %u Frequency: %lu MHz\n\n", cpu_index, cpu_cur); return 0; }
static gboolean cpufreq_selector_libcpufreq_set_frequency (CPUFreqSelector *selector, guint frequency, GError **error) { guint freq; guint cpu; g_object_get (G_OBJECT (selector), "cpu", &cpu, NULL); freq = cpufreq_selector_libcpufreq_get_valid_frequency (CPUFREQ_SELECTOR_LIBCPUFREQ (selector), frequency); if (cpufreq_set_frequency (cpu, freq) != 0) { g_set_error (error, CPUFREQ_SELECTOR_ERROR, SELECTOR_ERROR_SET_FREQUENCY, "Cannot set frequency '%d'", frequency); return FALSE; } return TRUE; }
JNIEXPORT jint JNICALL Java_platforms_x86_X86_1DVFS_setFrequency (JNIEnv *env, jobject obj, jint cpu, jlong freq) { int ret; ret = cpufreq_set_frequency((unsigned int)cpu, freq); return ret; }
int single_freq_act (actuator_t *act) { int err = 0; err = cpufreq_set_frequency(act->core, act->set_value); /* warning: cpufreq_set_frequency tries sysfs first, then proc; this means that if sysfs fails with EACCESS, the errno is then masked by the ENOENT from proc! */ act->value = cpufreq_get_freq_kernel(act->core); return err; }
int global_freq_act (actuator_t *act) { int err = 0; int cpu; for (cpu = 0; cpu < get_core_count(); cpu++) err = err || cpufreq_set_frequency(cpu, act->set_value); /* warning: cpufreq_set_frequency tries sysfs first, then proc; this means that if sysfs fails with EACCESS, the errno is then masked by the ENOENT from proc! */ act->value = cpufreq_get_freq_kernel(0); return err; }
static int do_one_cpu(unsigned int cpu, struct cpufreq_policy *new_pol, unsigned long freq, unsigned int pc) { switch (pc) { case 0: return cpufreq_set_frequency(cpu, freq); case 1: if (new_pol->min) return cpufreq_modify_policy_min(cpu, new_pol->min); else if (new_pol->max) return cpufreq_modify_policy_max(cpu, new_pol->max); else if (new_pol->governor) return cpufreq_modify_policy_governor(cpu, new_pol->governor); default: return do_new_policy(cpu, new_pol); } }
static int do_one_cpu(unsigned int cpu, struct cpufreq_policy *new_pol, unsigned long freq, unsigned int pc) { switch (pc) { case 0: return cpufreq_set_frequency(cpu, freq); case 1: /* if only one value of a policy is to be changed, we can * use a "fast path". */ if (new_pol->min) return cpufreq_modify_policy_min(cpu, new_pol->min); else if (new_pol->max) return cpufreq_modify_policy_max(cpu, new_pol->max); else if (new_pol->governor) return cpufreq_modify_policy_governor(cpu, new_pol->governor); default: /* slow path */ return do_new_policy(cpu, new_pol); } }
int main(int argc, char** argv) { int n = 0; int i; const int MAX = atoi(argv[1]); const int CORES = atoi(argv[2]); int ncpus=0; int apps[1024]; /**/ extern char *optarg; extern int optind, opterr, optopt; int ret = 0, cont = 1; unsigned int cpu = 0; //unsigned int cpu_defined = 0; unsigned long min_available_freq = 0; unsigned long max_available_freq = 0; unsigned long current_freq = 0; unsigned long initial_freq = 0; struct cpufreq_available_frequencies *freqs; int retr =0; setlinebuf(stdout); ncpus=get_cpus(); if(!CORES){ CORES == ncpus; } if (CORES > ncpus){ printf("Wrong number of inital cores"); exit(2); } ret= get_hardware_limits(cpu); min_available_freq = min; max_available_freq = max; ret= set_policy(CORES); freqs = cpufreq_get_available_frequencies(0); /*if (freqs==null){ goto out; }*/ current = get_available_freqs_size(freqs); unsigned long* available_freqs = (unsigned long *) malloc(current*sizeof(unsigned long)); ret = store_available_freqs( freqs, available_freqs, current); int current_counter = get_init_frequency(available_freqs, cpu); /*if (ret!=0){ goto out; }*/ current_freq = cpufreq_get_freq_kernel(cpu); if(getenv("HEARTBEAT_ENABLED_DIR") == NULL) { fprintf(stderr, "ERROR: need to define environment variable HEARTBEAT_ENABLED_DIR (see README)\n"); return 1; } heart_data_t* records = (heart_data_t*) malloc(MAX*sizeof(heart_data_t)); int last_tag = -1; while(n == 0) { n = get_heartbeat_apps(apps); } printf("apps[0] = %d\n", apps[0]); // For this test we only allow one heartbeat enabled app // assert(n==1); if (n>1) { printf("too many apps!!!!!!!!!!!!!!\n"); exit(2); } /* sleep(5);*/ #if 1 int rc = heart_rate_monitor_init(&heart, apps[0]); if (rc != 0) printf("Error attaching memory\n"); printf("buffer depth is %lld\n", (long long int) heart.state->buffer_depth); i = 0; printf(" rate interval is %f - %f\n", hrm_get_min_rate(&heart), hrm_get_max_rate(&heart)); printf("beat\trate\tfreq\tcores\ttact\twait\n"); int64_t window_size = hrm_get_window_size(&heart); int wait_for = (int) window_size; int current_beat = 0; int current_beat_prev= 0; int nprocs = 1; unsigned int set_freq = min; // return 1; while(current_beat < MAX) { int rc = -1; heartbeat_record_t record; char command[256]; while (rc != 0 || record.window_rate == 0.0000 ){ rc = hrm_get_current(&heart, &record); current_beat = record.beat; } if(current_beat_prev == current_beat) continue; /* printf(" rc: %d, current_beat:%d \n", rc,current_beat);*/ /*Situation where doesn't happen nothing*/ if( current_beat < wait_for){ current_beat_prev= current_beat; /* printf("I am in situation wait_for\n");*/ current_freq = cpufreq_get_freq_kernel(0); print_status(&record, wait_for, current_freq, '.', CORES); continue; } /* printf("Current beat is %d, wait_for = %d, %f\n", current_beat, wait_for, record.window_rate);*/ /*Situation where frequency is up-scaled*/ if(record.window_rate < hrm_get_min_rate(&heart)) { wait_for = current_beat + window_size; if (current_counter > 0){ int cpu; current_counter--; set_freq = get_speed(available_freqs[current_counter]); for (cpu=0; cpu <=CORES; cpu++) { /* sprintf(command, "cpufreq-set -c %d -f %luMHZ", cpu,set_freq);*/ /* printf("Executing %s\n", command);*/ /* system(command);*/ cpufreq_set_frequency(cpu, available_freqs[current_counter]); } current_freq = cpufreq_get_freq_kernel(0); print_status(&record, wait_for, current_freq, '+', CORES); } else { current_freq = cpufreq_get_freq_kernel(0); print_status(&record, wait_for, current_freq, 'M', CORES); } } /*Situation where frequency is downscaled*/ else if(record.window_rate > hrm_get_max_rate(&heart)) { wait_for = current_beat + window_size; if (current_counter < current){ current_counter++; set_freq = get_speed(available_freqs[current_counter]); for (cpu=0; cpu <=CORES; cpu++) { /* sprintf(command, " cpufreq-set -c %d -f %uMHZ", cpu,set_freq);*/ /* printf("Executing %s\n", command);*/ /*system(command);*/ cpufreq_set_frequency(cpu, available_freqs[current_counter]); } current_freq = cpufreq_get_freq_kernel(0); print_status(&record, wait_for, current_freq, '-', CORES); } else { current_freq = cpufreq_get_freq_kernel(0); print_status(&record, wait_for, current_freq, 'm', CORES); } } else { wait_for = current_beat+1; print_status(&record, wait_for, current_freq, '=', CORES); } current_beat_prev= current_beat; records[i].tag = current_beat; records[i].rate = record.window_rate; i++; } //printf("System: Global heart rate: %f, Current heart rate: %f\n", heart.global_heartrate, heart.window_heartrate); /* for(i = 0; i < MAX; i++) { printf("%d, %f\n", records[i].tag, records[i].rate); }*/ heart_rate_monitor_finish(&heart); #endif return 0; }