Changeset - dc7e1845745e
[Not reviewed]
default
0 1 0
LordOfWar - 16 years ago 2009-10-26 23:22:17

perfected new frame finder which is implimented into the user interface, it assumes that every time you get a frame, that you are a new slave with a render power of 1. I may draw I diagram of how it works later.
1 file changed with 36 insertions and 27 deletions:
0 comments (0 inline, 0 general)
src/server/distrend.c
Show inline comments
 
@@ -401,167 +401,175 @@ int find_jobframe(struct distrenjob *hea
 

	
 
      if(found)
 
    	break;
 
    }
 

	
 
    if(found)
 
      break;
 
  }
 

	
 
  if(!found)
 
    {
 
      fprintf(stderr, "No more jobs to render\n");
 
      sleep(1); /*< @todo eliminate the need for this line*/
 
      return 1;
 
    }
 

	
 
  *job = distrenjob_ptr;
 
  *frame = &distrenjob_ptr->frameset[frame_counter];
 

	
 
  return 0;
 
}
 

	
 
int find_jobframe_from_job(struct distrenjob *distrenjob_ptr, struct distrenjob **job, struct frameset **frame)
 
{
 
  unsigned int frame_counter;
 
  unsigned short int found;
 

	
 
  found = 0;
 
  for(frame_counter = (distrenjob_ptr->prev_frame_index + 1); frame_counter < distrenjob_ptr->total_frames; frame_counter ++)
 
  {
 
    if(distrenjob_ptr->frameset[frame_counter].status == FRAMESETSTATUS_UNASSIGNED) // jobframe found
 
      {
 
    	found = 1;
 
		distrenjob_ptr->frameset[frame_counter].status = FRAMESETSTATUS_ASSIGNED;
 
		distrenjob_ptr->frameset[frame_counter].start_time = clock();
 
		distrenjob_ptr->assigned_frames++;
 
		distrenjob_ptr->prev_frame_index = frame_counter;
 
		updateJobStatsXML(distrenjob_ptr);
 
      }
 

	
 
    if(found)
 
      break;
 
  }
 

	
 

	
 
  if(!found)
 
    {
 
      fprintf(stderr, "No more frames in this job number %d", distrenjob_ptr->jobnum);
 
      sleep(1); /*< @todo eliminate the need for this line*/
 
      distrenjob_ptr->prev_frame_index = frame_counter;
 
      return 1;
 
    }
 

	
 
  *job = distrenjob_ptr;
 
  *frame = &distrenjob_ptr->frameset[frame_counter];
 

	
 
  return 0;
 
}
 

	
 
// find a frame to render when the job that the last frame was for no longer exists
 
int find_jobframe_new(struct distrenjob *head, int rend_pwr, struct distrenjob **job, struct frameset **frame)
 
{
 
  if(general_info.hibernate)
 
    return 1;
 

	
 
  int power_difference;
 
  int greatest_power_difference;
 
  float power_difference;
 
  float greatest_power_difference;
 
  unsigned short int found;
 
  struct distrenjob *job_to_render;
 

	
 
  struct distrenjob *distrenjob_ptr;
 

	
 
  greatest_power_difference = 0;
 
  greatest_power_difference = -10000;
 
  found = 0;
 
  /* iterate through jobs from first to last */
 
  for(distrenjob_ptr = head->next;
 
  distrenjob_ptr && !distrenjob_ptr->hibernate && distrenjob_ptr->prev_frame_index < distrenjob_ptr->total_frames;
 
  distrenjob_ptr && !distrenjob_ptr->hibernate;
 
  distrenjob_ptr = distrenjob_ptr->next)
 
  {
 
	  power_difference = (general_info.total_render_power / general_info.total_priority_pieces)*(11-distrenjob_ptr->priority);
 
	  power_difference = power_difference - distrenjob_ptr->assigned_render_power;
 
	  if(distrenjob_ptr->prev_frame_index < (distrenjob_ptr->total_frames - 1))
 
	  {
 
		  power_difference = (((float)general_info.total_render_power / (float)general_info.total_priority_pieces)*(11-(float)distrenjob_ptr->priority));
 
		  power_difference = power_difference - (float)distrenjob_ptr->assigned_render_power;
 

	
 
		  fprintf(stderr, "job num %d\npower difference: %f\n", distrenjob_ptr->jobnum, power_difference);
 

	
 
	  if(power_difference > greatest_power_difference)
 
	  {
 
		  job_to_render = distrenjob_ptr;
 
		  greatest_power_difference = power_difference;
 
		  found = 1;
 
	  }
 
  }
 
  }
 

	
 
  if(!found)
 
    {
 
      fprintf(stderr, "No more jobs to render\n");
 
      sleep(1); /*< @todo eliminate the need for this line*/
 
      return 1;
 
    }
 

	
 
  find_jobframe_from_job(job_to_render, job, frame);
 
  job_to_render->assigned_render_power = job_to_render->assigned_render_power + rend_pwr;
 

	
 
  return 0;
 
}
 

	
 
// gets a frame to render from the same job that the previously rendered frame was from
 
int find_jobframe_again(struct distrenjob *head, int jobnum, int rend_pwr, struct distrenjob **job, struct frameset **frame)
 
{
 
  if(general_info.hibernate)
 
    return 1;
 

	
 
  short int found;
 
  struct distrenjob *distrenjob_ptr;
 

	
 
  distrenjob_ptr = distrenjob_get(head, jobnum);
 

	
 
  if(!distrenjob_ptr)
 
  // if the job was not found or there are no frames left in the job...
 
  if(!distrenjob_ptr || distrenjob_ptr->prev_frame_index >= (distrenjob_ptr->total_frames - 1))
 
    {
 
      fprintf(stderr, "Job number %d has been finished, finding new job\n", jobnum);
 
      find_jobframe_new(head, rend_pwr, job, frame);
 
      sleep(1); /*< @todo eliminate the need for this line*/
 
      return 1;
 

	
 
      // if previous job isn't yet finished the render power of the slave is removed from it
 
      if(distrenjob_ptr)
 
    	  distrenjob_ptr->assigned_render_power = distrenjob_ptr->assigned_render_power - rend_pwr;
 

	
 
      return find_jobframe_new(head, rend_pwr, job, frame);
 
    }
 

	
 
  found = 0;
 
  found = find_jobframe_from_job(distrenjob_ptr, job, frame);
 

	
 
  if(found)
 
	  find_jobframe_new(head, rend_pwr, job, frame);
 

	
 
  return 0;
 
}
 

	
 
/** Checks for dead, latent, or stale slaves */
 
void frame_watchdog(struct distrenjob *distrenjob_head)
 
{
 
  struct distrenjob *distrenjob_ptr;
 
  unsigned int counter;
 

	
 
  for(distrenjob_ptr = distrenjob_head->next; distrenjob_ptr; distrenjob_ptr = distrenjob_ptr->next)
 
  /* iterate through jobs */
 
  {
 
    /* if the job has been started, checks by seeing if either to first or second frame has been started */
 
    if(distrenjob_ptr->frameset[0].status != FRAMESETSTATUS_UNASSIGNED || distrenjob_ptr->frameset[1].status != FRAMESETSTATUS_UNASSIGNED)
 
      /* iterate through all frames for this job: */
 
      for(counter = 0; counter < distrenjob_ptr->total_frames; counter ++)
 
	    /*watchdog_forgiveness = seconds of forgiveness before frame is re-assigned: */
 
        if((distrenjob_ptr->frameset[counter].start_time + distrenjob_ptr->watchdog_forgiveness) < clock())
 
        {
 
            /*
 
            If frame is not completed within the number of seconds specified by watchdog_forgiveness
 
            Then change the frame status to unassigned
 
            */
 
            distrenjob_ptr->frameset[counter].status = FRAMESETSTATUS_UNASSIGNED;
 
            distrenjob_ptr->assigned_frames--;
 
        }
 

	
 
    updateJobStatsXML(distrenjob_ptr);
 
  }
 
}
 

	
 
/**
 
   Finds a distrenjob struct based on the jobnum
 
   @arg jobnum job number to search for
 
   @return NULL on job doesn't exist
 
 */
 
struct distrenjob *distrenjob_get(struct distrenjob *head, jobnum_t jobnum)
 
{
 
  struct distrenjob *distrenjob_ptr;
 

	
 
@@ -976,276 +984,277 @@ int reCreateQueueFromXML(struct distrenj
 
  return 1;
 
}
 

	
 
/* ************************** Test Functions ************************* */
 
int printFrameInfo(struct frameset *frame)
 
{
 
  char *status;
 

	
 
  status = NULL;
 

	
 
  switch(frame->status)
 
  {
 
    case FRAMESETSTATUS_UNASSIGNED:
 
      _distren_asprintf(&status, "%s", "unassigned");
 
      break;
 
    case FRAMESETSTATUS_ASSIGNED:
 
      _distren_asprintf(&status, "%s", "assigned");
 
      break;
 
    case FRAMESETSTATUS_DONE:
 
      _distren_asprintf(&status, "%s", "completed");
 
      break;
 
    case FRAMESETSTATUS_CANCELED:
 
      _distren_asprintf(&status, "%s", "canceled");
 
  }
 

	
 
  printf("frame #: %d --> %s\n", frame->num, status);
 

	
 
  return 1;
 
}
 

	
 
int printJob(struct distrenjob *job)
 
{
 
  int counter;
 
  fprintf(stderr, "frame_num: status\n");
 
  for(counter = 0; counter < job->total_frames; counter++)
 
    {
 
      printFrameInfo(&job->frameset[counter]);
 
    }
 

	
 
  return 1;
 
}
 

	
 
int printJobInfo(struct distrenjob *job)
 
{
 
  fprintf(stderr, "type: %d\n", job->type);
 
  fprintf(stderr, "name: %s\n", job->email);
 
  fprintf(stderr, "submitter: %s\n", job->submitter);
 
  fprintf(stderr, "e-mail: %s\n", job->email);
 
  fprintf(stderr, "priority, %d\n", job->priority);
 
  fprintf(stderr, "priority: %d\n", job->priority);
 
  fprintf(stderr, "completed: %d\n", job->completed_frames);
 
  fprintf(stderr, "assigned: %d\n", job->assigned_frames);
 
  fprintf(stderr, "total: %d\n", job->total_frames);
 
  fprintf(stderr, "watchdog: %d\n", job->watchdog_forgiveness);
 
  fprintf(stderr, "hibernate: %d\n", job->hibernate);
 
  fprintf(stderr, "prev_frame: %d\n", job->prev_frame_index);
 
  fprintf(stderr, "render power: %ld\n", job->assigned_render_power);
 

	
 
  return 1;
 
}
 

	
 
int printAllJobnums(struct distrenjob *head)
 
{
 
  struct distrenjob *current_job;
 
  int total_jobs;
 
  fprintf(stderr, "job numbers in the order they will be processed:\n");
 

	
 
  total_jobs = 0;
 
  for(current_job = head->next; current_job; current_job = current_job->next)
 
    {
 
      fprintf(stderr, "%d: %s\n", current_job->jobnum, current_job->name);
 
      total_jobs++;
 
    }
 

	
 
  fprintf(stderr, "%d jobs in queue\n", total_jobs);
 
  fprintf(stderr, "\n%d jobs in queue\n\n", total_jobs);
 

	
 
  return 1;
 
}
 

	
 
/* ************************** Main ************************* */
 

	
 
int main(int argc, char *argv[])
 
{
 

	
 
  /* Argument-parser */
 
  int counter;
 
  int test; // Have some fun if-ness that does fun stuff when this is 1
 

	
 

	
 
  struct distrenjob head;
 

	
 
  int cont;
 
  struct distrend_listenset *listenset;
 
  struct distrend_config *config;
 

	
 
  enum clientstatus
 
  {
 
    CLIENTSTATUS_UNINITIALIZED = 0,
 
    CLIENTSTATUS_BUSY = 1,
 
    CLIENTSTATUS_IDLE = 2
 
  } clientstatus;
 

	
 

	
 
  // initialize general_info struct, this should be done by start_data()
 
  general_info.jobs_in_queue = 0;
 
  general_info.free_clients = 0;
 
  general_info.rendering_clients = 0;
 
  general_info.total_finished_jobs = 0;
 
  general_info.total_frames_rendered = 0;
 
  general_info.highest_jobnum = 0;
 
  general_info.hibernate = 0;
 
  general_info.timestamp = 0;
 
  general_info.total_render_power = 0;
 
  general_info.total_priority_pieces = 0;
 

	
 
  int command;
 
  jobnum_t jobnum;
 
  struct distrenjob *tmp_job;
 
  struct frameset *tmp_frame;
 
  int type;
 
  char *name;
 
  char *submitter;
 
  char *email;
 
  int priority;
 
  int width;
 
  int height;
 
  int start_frame;
 
  int end_frame;
 
  size_t read_buf;
 

	
 
  head.priority = 0; // make head have the highest priority
 

	
 
  xmlinit();
 

	
 
  test = 0;
 
  for(counter = 0; counter < argc; counter ++)
 
    {
 
      if(strcmp(argv[counter], "-h") == 0)
 
      {
 
    	  fprintf(stderr, "Usage: distrend [option] \nStarts the distrend server\n\t-h\tshow this help\n\t-t\tlaunches queue testing interface \n");
 
		  return 2;
 
      }
 

	
 
      else if(strcmp(argv[counter], "-t") == 0)
 
      {
 
    	  fprintf(stderr, "Entering into test mode...\n\n");
 
		  test=1;
 
      }
 
    }
 
  cont = 1;
 
  memset(&head, '\0', sizeof(struct distrenjob));
 

	
 

	
 
  distrend_do_config(argc, argv, &config);
 

	
 
  //start_data(&head); // Starts fresh or loads data from xml dump. Should we grab the return?
 

	
 
  // pre-loaded jobs for testing
 
  prepare_distrenjob(&head, 1, "awesome", "LordOfWar", "onlylordofwar@gmail.com", 4, 1, 100, 640, 480);
 
  prepare_distrenjob(&head, 1, "awesome", "LordOfWar", "onlylordofwar@gmail.com", 8, 1, 100, 640, 480);
 
  prepare_distrenjob(&head, 1, "hamburger", "Ohnobinki", "ohnobinki@ohnopublishing.net", 3, 1, 50, 1280, 720);
 

	
 
  while(test == 1)
 
  {
 
    fprintf(stderr, "Welcome to DistRen Alpha Interactive Test Mode\n\n");
 
    fprintf(stderr, "\t1 \tPrint all frames in a job\n");
 
    fprintf(stderr, "\t2 \tExamine certain job\n");
 
    fprintf(stderr, "\t3 \tGet a frame to render\n");
 
    fprintf(stderr, "\t4 \tAdd a job\n");
 
    fprintf(stderr, "\t5 \tDelete a job\n");
 
    fprintf(stderr, "\t6 \tPrint jobnums in queue\n");
 
    fprintf(stderr, "\t7 \tQuit\n");
 
    fprintf(stderr, "\t7 \tPrint general info\n");
 
    fprintf(stderr, "\t8 \tQuit\n");
 

	
 
    scanf("%d", &command);
 

	
 
    switch(command)
 
    {
 
    case 1:
 
      fprintf(stderr, "Job number: ");
 
      scanf("%d", &jobnum);
 
      printJob(distrenjob_get(&head, jobnum));
 
      break;
 
    case 2:
 
      fprintf(stderr, "Job number: ");
 
      scanf("%d", &jobnum);
 
      printJobInfo(distrenjob_get(&head, jobnum));
 
      break;
 
    case 3:
 
      fprintf(stderr, "\nPut the number of a non existing job to get a new frame\nJob number: ");
 
      scanf("%d", &jobnum);
 
      fprintf(stderr, "\nRender Power: ");
 
      scanf("%d", &priority);
 
      fprintf(stderr, "\n1->new user (Render Power has not been added to general_info)\n2->returning user\ninput:");
 
      scanf("%d", &start_frame);
 

	
 
      if(start_frame == 1)
 
    	  general_info.total_render_power = general_info.total_render_power + priority;
 

	
 
      if(!find_jobframe_again(&head, jobnum, priority, &tmp_job, &tmp_frame))
 
      general_info.total_render_power++;
 
      if(!find_jobframe_again(&head, -1, 1, &tmp_job, &tmp_frame))
 
      {
 
    	  fprintf(stderr, "frame was found, details below\n");
 
          fprintf(stderr, "Job#:%d\n", tmp_job->jobnum);
 
          fprintf(stderr, "Frame#:%d\n", tmp_frame->num);
 
      }
 

	
 
      break;
 
    case 4:
 
      name = NULL;
 
      submitter = NULL;
 
      email = NULL;
 

	
 
      fprintf(stderr, "\nType: \n\t 1 \t blender\n\t 2 \t povray\n"); scanf("%d", &type);
 
      fprintf(stderr, "\nName: ");        scanf("\n"); getline(&name, &read_buf, stdin);
 
      fprintf(stderr, "\nSubmitter: ");                getline(&submitter, &read_buf, stdin);
 
      fprintf(stderr, "\nEmail: ");                    getline(&email, &read_buf, stdin);
 
      fprintf(stderr, "\nPriority: ");                 scanf("%d", &priority);
 
      fprintf(stderr, "\nStart frame: ");              scanf("%d", &start_frame);
 
      fprintf(stderr, "\nEnd frame: ");                scanf("%d", &end_frame);
 
      fprintf(stderr, "\nWidth: ");                    scanf("%d", &width);
 
      fprintf(stderr, "\nHeight: ");                   scanf("%d", &height);
 
      prepare_distrenjob(&head, type, name, submitter, email, priority, start_frame, end_frame, width, height);
 
      break;
 
    case 5:
 
      fprintf(stderr, "\nJob number: ");
 
      scanf("%d", &jobnum);
 
      distrenjob_remove(&head, distrenjob_get(&head, jobnum));
 
      break;
 
    case 6:
 
      printAllJobnums(&head);
 
      break;
 
    case 7:
 
      fprintf(stderr, "\nHighest job number: %d", general_info.highest_jobnum);
 
      fprintf(stderr, "\nJobs in queue: %d", general_info.jobs_in_queue);
 
      fprintf(stderr, "\nTotal frames rendered: %d", general_info.total_frames_rendered);
 
      fprintf(stderr, "\nTimestamp: %d", general_info.timestamp);
 
      fprintf(stderr, "\nTotal priority pieces: %ld", general_info.total_priority_pieces);
 
      fprintf(stderr, "\nTotal render power: %ld\n", general_info.total_render_power);
 
      break;
 
    case 8:
 
      fprintf(stderr,"Goodbye.\n");
 
      return 0;
 
    default:
 
      fprintf(stderr, "Invalid input, please try again.\n");
 
    }
 
  }
 

	
 
  distrend_listen(&listenset, config);
 
  /* This is called the "main loop" */
 
  while(cont)
 
    {
 
      struct distren_action *action;
 
      int clientsays = 0; /*< temporary example variable, will be replaced when we can handle messages */
 

	
 
      distrend_accept(&action);
 
      cont = distrend_do(action);
 

	
 
      /* Make the following code more event-driven */
 
      frame_watchdog(&head);
 

	
 

	
 
      struct frameset *frame;
 
      struct distrenjob *job;
 

	
 
      /* If the client is idle, must be modified for climbing through linked list of clients (client->clientnum) */
 
      if(clientstatus == CLIENTSTATUS_IDLE)
 
	{
 
	  int returnnum = find_jobframe(&head, &job, &frame); // Finds a frame to render
 
	  if(returnnum)
 
	    {
 
	      fprintf(stderr,"No frames are available to render at this time. Idling...\n");
 
	      sleep(10);
 
	    }
 
	  else
 
	    remotio_send_to_client(frame->num, job->jobnum); // Pseudo-sends data to client
 
	}
 
      /* If the client states that they finished the frame */
 
      	if(clientsays == DISTREN_REQUEST_DONEFRAME){
 
      	  clientstatus = CLIENTSTATUS_IDLE; // Sets the client back to idle
 
      	  finish_frame(&head, job, frame->num); // @TODO: Make sure this actually works.
 
      	}
 

	
 
      distrend_action_free(action);
 
    }
 

	
 
  distrend_unlisten(listenset);
 
  distrend_config_free(config);
 

	
0 comments (0 inline, 0 general)