/** * int NFU(Algorithm_Data *data) * * NFU Page Replacement Algorithm * * @param *data {Algorithm_Data} struct holding algorithm data * * return {int} did page fault, 0 or 1 */ int NFU(Algorithm_Data *data) { struct Frame *framep = data->page_table.lh_first, *victim = NULL; int fault = 0; /* Find target (hit), empty page index (miss), or victim to evict (miss) */ while (framep != NULL && framep->page > -1 && framep->page != last_page_ref) { if(victim == NULL || framep->extra < victim->extra) victim = framep; // No victim or frame used fewer times framep = framep->frames.le_next; } /* Make a decision */ if(framep == NULL) { // It's a miss, kill our victim add_victim(&data->victim_list, victim); victim->page = last_page_ref; time(&victim->time); victim->extra = 0; fault = 1; } else if(framep->page == -1) { // Can use free page table index framep->page = last_page_ref; time(&framep->time); framep->extra = 0; fault = 1; } else if(framep->page == last_page_ref) { // The page was found! Hit! time(&framep->time); framep->extra++; } if(fault == 1) data->misses++; else data->hits++; return fault; }
/** * int CLOCK(Algorithm_Data *data) * * CLOCK Page Replacement Algorithm * * @param *data {Algorithm_Data} struct holding algorithm data * * return {int} did page fault, 0 or 1 */ int CLOCK(Algorithm_Data *data) { static Frame *clock_hand = NULL; // Clock needs a hand Frame *framep = data->page_table.lh_first; int fault = 0; /* Forward traversal. */ /* Find target (hit), empty page slot (miss), or victim to evict (miss) */ while(framep != NULL && framep->page > -1 && framep->page != last_page_ref) framep = framep->frames.le_next; /* Make a decision */ if(framep != NULL) { if(framep->page == -1) { framep->page = last_page_ref; framep->extra = 0; fault = 1; } else { // Found the page, update its R bit to 0 framep->extra = 0; } } else // Use the hand to find our victim { while(clock_hand == NULL || clock_hand->extra == 0) { if(clock_hand == NULL) { clock_hand = data->page_table.lh_first; } else { clock_hand->extra = 1; clock_hand = clock_hand->frames.le_next; } } add_victim(&data->victim_list, clock_hand); clock_hand->page = last_page_ref; clock_hand->extra = 0; fault = 1; } if(fault == 1) data->misses++; else data->hits++; return fault; }
/** * int RANDOM(Algorithm_Data *data) * * RANDOM Page Replacement Algorithm * * @param *data {Algorithm_Data} struct holding algorithm data * * return {int} did page fault, 0 or 1 */ int RANDOM(Algorithm_Data *data) { struct Frame *framep = data->page_table.lh_first, *victim = NULL; int rand_victim = rand() % num_frames; int fault = 0; /* Find target (hit), empty page index (miss), or victim to evict (miss) */ while (framep != NULL && framep->page > -1 && framep->page != last_page_ref) { if(framep->index == rand_victim) // rand victim = framep; framep = framep->frames.le_next; } if(framep == NULL) { // It's a miss, kill our victim if(debug) printf("Victim selected: %d, Page: %d\n", victim->index, victim->page); add_victim(&data->victim_list, victim); victim->page = last_page_ref; time(&victim->time); victim->extra = counter; fault = 1; } else if(framep->page == -1) { // Use free page table index framep->page = last_page_ref; time(&framep->time); framep->extra = counter; fault = 1; } else if(framep->page == last_page_ref) { // The page was found! Hit! time(&framep->time); framep->extra = counter; } if(debug) { printf("Page Ref: %d\n", last_page_ref); for (framep = data->page_table.lh_first; framep != NULL; framep = framep->frames.le_next) printf("Slot: %d, Page: %d, Time used: %d\n", framep->index, framep->page, framep->extra); } if(fault == 1) data->misses++; else data->hits++; return fault; }
/** * int FIFO(Algorithm_Data *data) * * FIFO Page Replacement Algorithm * * @param *data {Algorithm_Data} struct holding algorithm data * * return {int} did page fault, 0 or 1 */ int FIFO(Algorithm_Data *data) { struct Frame *framep = data->page_table.lh_first, *victim = NULL; int fault = 0; /* Find target (hit), empty page index (miss), or victim to evict (miss) */ while (framep != NULL && framep->page > -1 && framep->page != last_page_ref) { if(victim == NULL || framep->time > victim->time) { // No victim yet or frame older than victim victim = framep; } framep = framep->frames.le_next; } /* Make a decision */ if(framep == NULL) { // It's a miss, kill our victim if(debug) printf("Victim selected: %d, Page: %d\n", victim->index, victim->page); add_victim(&data->victim_list, victim); victim->page = last_page_ref; time(&victim->time); victim->extra = counter; fault = 1; } else if(framep->page == -1) { // Can use free page table index framep->page = last_page_ref; time(&framep->time); framep->extra = counter; fault = 1; } else if(framep->page == last_page_ref) { // The page was found! Hit! time(&framep->time); framep->extra = counter; } if(fault == 1) data->misses++; else data->hits++; return fault; }
/* * This example shows how to use the C "client" API. * It essentially wraps a subset of the JSON API, * relevant for a SHERPA mission. * * For communication zyre is used. * Please make ensure a correct zyre configuration file * is passed. */ int main(int argc, char *argv[]) { char agent_name[] = "fw0"; // or wasp1, operator0, ... donkey0, sherpa_box0. Please use the same as SWM_AGENT_NAME environment variable. /* Load configuration file for communication setup */ char config_folder[255] = { SWM_ZYRE_CONFIG_DIR }; char config_name[] = "swm_zyre_config.json"; char config_file[512] = {0}; snprintf(config_file, sizeof(config_file), "%s/%s", config_folder, config_name); if (argc == 2) { // override default config snprintf(config_file, sizeof(config_file), "%s", argv[1]); } json_t * config = load_config_file(config_file);//"swm_zyre_config.json"); if (config == NULL) { return -1; } /* Spawn new communication component */ component_t *self = new_component(config); if (self == NULL) { return -1; } printf("[%s] component initialized!\n", self->name); char *msg; /* Input variables */ double x = 979875; double y = 48704; double z = 405; double utcTimeInMiliSec = 0.0; int i; struct timeval tp; printf("###################### CONNECTIVITY #########################\n"); char *root_id = 0; /* Get the root node ID of the local SHWRPA World Model. * This can be used the check connectivity to the SMW. * Typically false means the local SWM cannot be reached. Is it actually started? */ assert(get_root_node_id(self, &root_id)); free(root_id); printf("###################### AGENT #########################\n"); /* column-major layout: * 0 4 8 12 * 1 5 9 13 * 2 6 10 14 * 3 7 11 15 * * <=> * * r11 r12 r13 x * r21 r22 r23 y * r31 r32 r33 z * 3 7 11 15 */ double matrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // y,x,z,1 remember this is column-major! matrix[12] = x; matrix[13] = y; matrix[14] = z; assert(add_agent(self, matrix, utcTimeInMiliSec, agent_name)); assert(add_agent(self, matrix, utcTimeInMiliSec, agent_name)); // twice is not a problem, sine it checks for existance /* * Add new observations for potential victims */ for (i = 0; i < 2; ++i) { printf("###################### VICTIM #########################\n"); gettimeofday(&tp, NULL); utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds double matrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // y,x,z,1 remember this is column-major! matrix[12] = x; matrix[13] = y; matrix[14] = z; assert(add_victim(self, matrix, utcTimeInMiliSec, agent_name)); } /* * Add new image observations */ for (i = 0; i < 2; ++i) { printf("###################### IMAGE #########################\n"); gettimeofday(&tp, NULL); utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds double matrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // y,x,z,1 remember this is column-major! matrix[12] = x; matrix[13] = y; matrix[14] = z; assert(add_image(self, matrix, utcTimeInMiliSec, agent_name, "/tmp/image001.jpg")); } /* * Add new ARTVA observations (Only relevant for the WASPS) */ for (i = 0; i < 2; ++i) { printf("###################### ARTVA #########################\n"); gettimeofday(&tp, NULL); utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds double matrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // y,x,z,1 remember this is column-major! matrix[12] = x; matrix[13] = y; matrix[14] = z; assert(add_artva(self, matrix, 77, 12, 0, 0, utcTimeInMiliSec, agent_name)); } /* * Add new battery values. In fact it is stored in a single battery node and * get updated after first creation. */ for (i = 0; i < 2; ++i) { printf("###################### BATTERY #########################\n"); double voltage = 20 + i; assert(add_battery(self, voltage, "HIGH", utcTimeInMiliSec, agent_name)); } /* * Update pose of this agent */ for (i = 0; i < 30; ++i) { printf("###################### POSE #########################\n"); gettimeofday(&tp, NULL); utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds double matrix[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; // y,x,z,1 remember this is column-major! matrix[12] = x; matrix[13] = y; matrix[14] = z+i; update_pose(self, matrix, utcTimeInMiliSec+i, agent_name); usleep(100/*[ms]*/ * 1000); } /* * Get current position */ printf("###################### GET POSITION #########################\n"); x = 0; y = 0; z = 0; gettimeofday(&tp, NULL); utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds get_position(self, &x, &y, &z, utcTimeInMiliSec, agent_name); printf ("Latest position of agent = (%f,%f,%f)\n", x,y,z); /* * Get ID of mediator */ printf("###################### GET MEDIATOR ID #########################\n"); char* mediator_id = NULL; assert(get_mediator_id(self, &mediator_id)); printf ("ID of mediator = %s\n", mediator_id); free(mediator_id); printf("###################### DONE #########################\n"); /* Clean up */ destroy_component(&self); printf ("SHUTDOWN\n"); return 0; }
/** * int OPTIMAL(Algorithm_Data *data) * * OPTIMAL Page Replacement Algorithm * * @param *data {Algorithm_Data} struct holding algorithm data * * return {int} did page fault, 0 or 1 */ int OPTIMAL(Algorithm_Data *data) { Frame *framep = data->page_table.lh_first, *victim = NULL; int fault = 0; /* Find target (hit), empty page index (miss), or victim to evict (miss) */ while (framep != NULL && framep->page > -1 && framep->page != last_page_ref) { framep = framep->frames.le_next; } if(framep == NULL) { // It's a miss, find our victim size_t i,j; for(i = 0; i < page_ref_upper_bound; ++i) optimum_find_test[i] = -1; Page_Ref *page = page_refs.lh_first; int all_found = 0; j = 0; //optimum_find_test = malloc(sizeof(int)*page_ref_upper_bound); while(all_found == 0) { if(optimum_find_test[page->page_num] == -1) optimum_find_test[page->page_num] = j++; all_found = 1; for(i = 0; i < page_ref_upper_bound; ++i) if(optimum_find_test[i] == -1) { all_found = 0; break; } page = page->pages.le_next; } framep = data->page_table.lh_first; while (framep != NULL) { if(victim == NULL || optimum_find_test[framep->page] > optimum_find_test[victim->page]) { // No victim yet or page used further in future than victim victim = framep; } framep = framep->frames.le_next; } if(debug) printf("Victim selected: %d, Page: %d\n", victim->index, victim->page); add_victim(&data->victim_list, victim); victim->page = last_page_ref; time(&victim->time); victim->extra = counter; fault = 1; } else if(framep->page == -1) { // Use free page table index framep->page = last_page_ref; time(&framep->time); framep->extra = counter; fault = 1; } else if(framep->page == last_page_ref) { // The page was found! Hit! time(&framep->time); framep->extra = counter; } if(debug) { printf("Page Ref: %d\n", last_page_ref); for (framep = data->page_table.lh_first; framep != NULL; framep = framep->frames.le_next) printf("Slot: %d, Page: %d, Time used: %d\n", framep->index, framep->page, framep->extra); } if(fault == 1) data->misses++; else data->hits++; return fault; }