void core_controller (heartbeat_record_t *current, int act_count, actuator_t *acts) { static actuator_t *core_act = NULL; double target_rate = (hrm_get_max_rate(&hrm) + hrm_get_min_rate(&hrm)) / 2.0; double error = target_rate - current->window_rate; double Kp = 0.4; if (!core_act) get_actuators(&core_act, NULL, 0, NULL, NULL); core_act->set_value = core_act->value + Kp*error; if (core_act->set_value < core_act->min) core_act->set_value = core_act->min; else if (core_act->set_value > core_act->max) core_act->set_value = core_act->max; }
void core_heuristics (heartbeat_record_t *current, int act_count, actuator_t *acts) { static actuator_t *core_act = NULL; if (!core_act) get_actuators(&core_act, NULL, 0, NULL, NULL); if (current->window_rate < hrm_get_min_rate(&hrm)) { if (core_act->value < core_act->max) core_act->set_value++; } else if(current->window_rate > hrm_get_max_rate(&hrm)) { if (core_act->value > core_act->min) core_act->set_value--; } }
void machine_state_controller (heartbeat_record_t *current, int act_count, actuator_t *acts) { static actuator_t *speed_act = NULL; double target_rate = (hrm_get_max_rate(&hrm) + hrm_get_min_rate(&hrm)) / 2.0; double error = target_rate - current->window_rate; double Kp = 100; if (!speed_act) get_actuators(NULL, NULL, 0, NULL, &speed_act); speed_act->set_value = speed_act->value + Kp*error; #if DEBUG printf("target: %f hr: %f error: %f speed: %d -> %d ", target_rate, current->window_rate, error, speed_act->value, speed_act->set_value); #endif if (speed_act->set_value < speed_act->min) speed_act->set_value = speed_act->min; else if (speed_act->set_value > speed_act->max) speed_act->set_value = speed_act->max; #if DEBUG printf("clipped: %d\n", speed_act->set_value); #endif }
void step_heuristics (heartbeat_record_t *current, int act_count, actuator_t *acts) { static actuator_t *core_act = NULL; static actuator_t *freq_acts[16]; int last_core; freq_scaler_data_t *freq_data; if (!core_act) { get_actuators(&core_act, NULL, 16, &freq_acts[0], NULL); if (core_act->max > 16) exit(2); } last_core = core_act->value - 1; freq_data = freq_acts[last_core]->data; if (current->window_rate < hrm_get_min_rate(&hrm)) { if (freq_data->cur_index > 0) { /* increase last core's frequency if possible */ freq_data->cur_index--; freq_acts[last_core]->set_value = freq_data->freq_array[freq_data->cur_index]; } else if (last_core < core_act->max - 1) { /* else, add another core... */ core_act->set_value = core_act->value + 1; last_core++; /* ...at the lowest initial frequency */ freq_data = freq_acts[last_core]->data; freq_data->cur_index = freq_data->freq_count-1; freq_acts[last_core]->set_value = freq_data->freq_array[freq_data->cur_index]; } } else if(current->window_rate > hrm_get_max_rate(&hrm)) { if (freq_data->cur_index < freq_data->freq_count-1) { /* decrease last core's frequency if possible */ freq_data->cur_index++; freq_acts[last_core]->set_value = freq_data->freq_array[freq_data->cur_index]; } else if (last_core > core_act->min - 1) { /* else, reduce core count */ core_act->set_value = core_act->value - 1; last_core--; /* the core that is now last should already be at max frequency */ } } }
void freq_heuristics (heartbeat_record_t *current, int act_count, actuator_t *acts) { static actuator_t *freq_act = NULL; freq_scaler_data_t *freq_data; if (!freq_act) get_actuators(NULL, &freq_act, 0, NULL, NULL); freq_data = freq_act->data; if (current->window_rate < hrm_get_min_rate(&hrm)) { if (freq_data->cur_index > 0) { freq_data->cur_index--; freq_act->set_value = freq_data->freq_array[freq_data->cur_index]; } } else if(current->window_rate > hrm_get_max_rate(&hrm)) { if (freq_data->cur_index < freq_data->freq_count-1) { freq_data->cur_index++; freq_act->set_value = freq_data->freq_array[freq_data->cur_index]; } } }
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; }
int main(int argc, char** argv) { int n = 0; int i; const int MAX = atoi(argv[1]); int apps[1024]; 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); 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; int64_t window_size = hrm_get_window_size(&heart); int wait_for = (int) window_size; int current_beat = 0; int nprocs = 1; printf("Current beat is %d, wait_for = %d\n", current_beat, wait_for); // return 1; while(current_beat < MAX-1) { 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 < wait_for) continue; printf("Current beat is %d, wait_for = %d, %f\n", current_beat, wait_for, record.window_rate); if(record.window_rate < hrm_get_min_rate(&heart)) { nprocs++; sprintf(command, "taskset -pc 0-%d %d >& /dev/null",nprocs-1,apps[0]); printf("Executing %s\n", command); system(command); wait_for = current_beat + window_size; } else if(record.window_rate > hrm_get_max_rate(&heart)) { nprocs--; sprintf(command, "taskset -pc 0-%d %d >& /dev/null",nprocs-1,apps[0]); system(command); wait_for = current_beat + window_size; } else { wait_for = current_beat+1; } 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; }