Changeset - 1f986f0a72d1
[Not reviewed]
default
0 1 0
Nathan Brink (binki) - 16 years ago 2009-07-24 20:30:00
ohnobinki@ohnopublishing.net
struct blendjob*-ized frame_finder()
1 file changed with 63 insertions and 42 deletions:
0 comments (0 inline, 0 general)
src/server/distrend.c
Show inline comments
 
@@ -199,239 +199,260 @@ void status_report_generator(struct blen
 
    {
 
      if(blendjob_ptr->priority != 0)
 
	/* If the job is not done, scan it */
 
	{
 
	  unsigned int framecounter;  /*< to scan through frames */
 
	  unsigned long finished_frames; /*< variable that counts the completed frames */
 
	  unsigned int pending_frames; /*< variable that counts the assigned frames */
 
	  float percent_frames_finished;  /*< variable that stores the percent done of the blendjob */
 
	  unsigned int total_time;  /*< total time taken to render all the completed frames for a job */
 

	
 
	  framecounter = 0;
 
	  finished_frames = 0;
 
	  pending_frames = 0;
 
	  percent_frames_finished = 0;
 
	  total_time = 0;
 

	
 
	  while(framecounter < blendjob_ptr->total_frames)
 
	    /* scans through frames, based on their status it runs a statement(s) */
 
	    {
 
	      if(blendjob_ptr->frameset[framecounter].frame_status == 2)
 
		/* If the frame is done */
 
		{
 
		  finished_frames ++;
 
		  total_time += blendjob_ptr->frameset[framecounter].time_to_render;
 
		}
 
	      
 
	      if(blendjob_ptr->frameset[framecounter].frame_status == 1)
 
		/* If the frame is assigned */
 
		{
 
		  pending_frames ++;
 
		  workers_working ++;
 
		}
 
	      
 
	      framecounter ++;
 
	    } /* while(framecounter < blendjob_ptr->total_frames) */
 

	
 
	  // find the percent of completed frames
 
	  percent_frames_finished = (finished_frames / blendjob_ptr->total_frames) * 100; /*< @LordofWar: extraneous parentheses! */
 

	
 
	  /* updates values in the blendjob struct */
 
	  blendjob_ptr->completed_frames = finished_frames;
 
	  blendjob_ptr->assigned_frames = pending_frames;
 
	  blendjob_ptr->percent_done = percent_frames_finished;
 
	  blendjob_ptr->avg_render_time = (total_time / finished_frames); /*< extraneous parentheses! */
 
	  blendjob_ptr->time_remaining = (blendjob_ptr->avg_render_time * (blendjob_ptr->total_frames - finished_frames)); /*< extraneous parentheses! */
 

	
 
	  if(finished_frames == blendjob_ptr->total_frames)
 
	    /* If all frames are complete */
 
	    {
 
	      blendjob_ptr->priority = 0; /*< set priority to zero to indicate job is complete */
 
	      blendjob_remove(blendjobs_head, blendjob_ptr); /*< remove this job from the linkedlist */
 
	      general_info.total_finished_jobs++; /*< add one to the total finished jobs */
 
	      
 
	    }
 
	  else if (finished_frames > blendjob_ptr->total_frames)
 
	    /* just in case ;-) */
 
	    {
 
	      fprintf(stderr, "%s:%d: finished_frames (%lu) > blendjob_ptr->total_frames (%d)",
 
		      __FILE__, __LINE__, 
 
		      finished_frames, 
 
		      blendjob_ptr->total_frames);
 
	      abort();
 
	    }
 
	}
 

	
 
      general_info.rendering_clients = workers_working; /*< should this be a +=? */
 
      
 
      blendjob_ptr = blendjob_ptr->next; /*< This is the essence of linked lists and iterating through them */
 
      numjobs ++;
 
    } /* while(blendjob_ptr) */
 
  
 
  general_info.jobs_in_queue = (highest_jobnum - general_info.total_finished_jobs); /*< extraneous parentheses! */
 
}
 

	
 

	
 
// **** Structure Builder: This function creates frame array based on the total number of frames to be rendered, which will then be parsed by function frame_farmer.
 
void frame_num_struct_builder(struct blendjob *job, unsigned int startframe, unsigned int numframes) {
 
  int jobnum_new = highest_jobnum + 1; /* global vars will someday leave us  */
 
  unsigned int counter;
 

	
 
	job->frameset = malloc(sizeof(struct frameset) * numframes);
 
	if(!job->frameset)
 
	  fprintf(stderr, "error allocating memory");
 

	
 
	job->total_frames = numframes; // sets the total number of frames in animation for status purposes
 
	job->jobnum = jobnum_new;
 

	
 
	for(counter = 0; counter < numframes; counter ++)
 
	  /* This builds the array, with the array starting at zero and the frame_num starting at sframe */
 
	  job->frameset[counter].frame_num = counter + startframe;
 

	
 
	highest_jobnum++; // After it has created the job, it adds one to the highest_jobnum interger
 
}
 

	
 

	
 

	
 
// Frame Assigner: matches your computer up with a lovely frame to render
 
// Major issue here, the client needs to know the frame number, AND the job number!
 
int frame_finder(){
 
	int your_frame = 0;  // your_frame is an interger value that will be given to the client as the frame number to render
 
	int finder_jobnum = 0;
 
	int frameset_count = 0; // the frameset number, note* frames in an animation don't start at zero
 
	short int done = 0;
 
	short int priority = 10;
 

	
 
	while(priority >= 1){ // start the scan for the next job with the highest priority, decreases priority before it loops
 
		finder_jobnum = hcfjob + 1; // reset it to start scanning at first uncompleted job for the pass at each priority level
 

	
 
		while(finder_jobnum <= highest_jobnum){  // This keeps increasing the finder_jobnum until it is higher than the highest_jobnum
 
			if(blendjob[finder_jobnum].priority == priority){  // looks for a job with the current priority value
 
				done = 1;									  // notice it starts by looking at the oldest job first
 
				break;
 
			}
 

	
 
			if((done) == 1)  // If it has found a job with the current priority value, it will break out of the loop
 
				break;    // If none is found it goes to the next job to see if it is of the current priority value
 
			else
 
				finder_jobnum++;
 
		} // End of increasing finder_jobnum
 
/**
 
  Frame Finder: matches your computer up with a lovely frame to render
 
  Major issue here, the client needs to know the frame number, AND the job number!
 
  Notice that this function starts by looking at the oldest job first
 

	
 
		if(done == 1) // if job has been found, it lets it out of the priority changer loop
 
			break;
 

	
 
		priority--; // end of decreasing priority
 
	}
 

	
 
	while(your_frame < blendjob[finder_jobnum].total_frames){ // Finds the frameset number with a frame that needs to be rendered
 
		if (blendjob[finder_jobnum].frameset[frameset_count].frame_status == 0)  // If frame that is not assigned has been found, frameset_count is not changed
 
			break;																// and frameset_count is used to give the frame number later in this funciton
 

	
 
		frameset_count++;  // If frame is assigned or done, it goes to next frame
 
  @return 0 success, other: error
 
*/
 
int frame_finder(struct blendjob *head, struct blendjob **job, struct frameset **frame)
 
{
 
  int your_frame;  // your_frame is an interger value that will be given to the client as the frame number to render
 
  
 
  unsigned short int found;
 
  unsigned short int priority;
 
  
 
  struct blendjob *blendjob_ptr;
 
  
 
  found = 0;
 
  while(!found)
 
    {
 
      /* enumerate through priority levels */
 
      for(priority = 10;
 
	  priority > 0
 
	    && !found; 
 
	  priority --)
 
	/* Find the job with the highest priority */
 
	for(blendjob_ptr = head; 
 
	    blendjob_ptr != NULL 
 
	      && !found; 
 
	    blendjob_ptr = blendjob_ptr->next)
 
	  if(blendjob_ptr->priority == priority)
 
	    found = 1;
 
      
 
      if(!found)
 
	{
 
	  fprintf(stderr, "out of jobs to render\n");
 
	  return 1;
 
	}
 

	
 
blendjob[jobnum].frameset[frameset_count].frame_status++; // sets the value of the frame to 2, which means its taken
 

	
 
your_frame = blendjob[jobnum].frameset[frameset_count].frame_num; //  Takes the frameset found in the while statement above, and extracts the frame number from it and assigns it to the int your_frame
 
      
 
      found = 0;
 
      for(your_frame = 0; 
 
	  your_frame < blendjob_ptr->total_frames;
 
	  your_frame ++)
 
	if(blendjob_ptr->frameset[your_frame].frame_status == 0)
 
	  found = 1;
 
      
 
      if(!found)
 
	{
 
	  /* there are no frames left in this job */
 
	  blendjob_ptr->priority --;
 
	  
 
	  /* If that job had no open frames for some reason, run the status report generator so that */
 
	  status_report_generator(&head);
 

	
 
if(your_frame == 0)  // If that job had no open frames for some reason, run the status report generator so that
 
	status_report_generator(blendjobs_head);  //the job priority can be changed to 0
 
	  /* should the job be removed now? */
 
	  fprintf(stderr, "Job %d is finished, this is probably the place to call the job-removal function\n", blendjob_ptr->jobnum);
 
	}
 
    } /* while(!found) */
 
  
 
  fprintf(stderr, "Missing apostrophe !!!!!!!!!!!!!!\n"); abort();
 
  /* sets the value of the frame to 1, which means its taken !!!!!! MISSSING APOSTROPHE!!!!!!! */
 
  blendjob_ptr->frameset[your_frame].frame_status++; 
 
  
 
  blendjob_ptr->frameset[your_frame].start_time = clock();
 

	
 
blendjob[jobnum].frameset[frameset_count].start_time = clock();
 
  *job = blendjob_ptr;
 
  *frame =  &blendjob_ptr->frameset[your_frame];
 

	
 
return your_frame; // your_frame is returned as the frame to be rendered
 
  return 0;
 
}
 

	
 
void blend_frame_watchdog(struct blendjob *blendjob_head)
 
{
 
  unsigned short int watchdog_forgiveness; /*< seconds to wait on a frame before re-assigning it */
 
  struct blendjob *blendjob_ptr;
 
  unsigned int counter;
 

	
 
  watchdog_forgiveness = 3; /*< hours of forgiveness before frame is re-assigned */
 
  blendjob_ptr = blendjob_head;
 

	
 
  for(blendjob_ptr = blendjob_head; blendjob_ptr; blendjob_ptr = blendjob_ptr->next)
 
    /* iterate through jobs */
 

	
 
    for(counter = 0; counter < blendjob_ptr->total_frames; counter ++)
 
      /* iterate through all frames for this job*/
 
      {
 
	if((blendjob_ptr->frameset[counter].start_time + (watchdog_forgiveness * 3600)) < clock()) 
 
	  /*
 
	    If frame is not completed within the number of hours specified by watchdog_forgiveness 
 
	    Then change the frame status to unassigned
 
	  */
 
	  blendjob_ptr->frameset[counter].frame_status = 0;
 
      }
 

	
 
}
 

	
 
/**
 
   Finds a blendjob struct based on the jobnum
 
   @arg jobnum job number to search for
 
   @return NULL on job doesn't exist
 
 */
 
struct blendjob *blendjob_get(struct blendjob *head, jobnum_t jobnum)
 
{
 
  struct blendjob *blendjob_ptr;
 
  
 
  /*
 
    The conditions of the for loop will leave blendjob_ptr at NULL if the end of the list is reached. It will leave it pointing to the correct job if it is found.
 
   */
 
  for(blendjob_ptr = head;
 
      blendjob_ptr
 
	&& blendjob_ptr->jobnum != jobnum;
 
      blendjob_ptr = blendjob_ptr->next);
 
  
 
  return blendjob_ptr;
 
}
 

	
 

	
 
/**
 
   Removes a blendjob from the blendjob linkelist.
 

	
 
   @arg head a double pointer. the head pointer will have to be changed if blendjob == *head. Thus, make sure that the pointer points to the pointer to the head that all functions use. (I'm going to come back to this and misunderstand myself ;-))
 
 */
 
void blendjob_remove(struct blendjob **head, struct blendjob *bj)
 
{
 
  struct blendjob *previous_blendjob;
 
  
 
  if(bj == *head)
 
    *head = bj->next;
 
  else
 
    {
 
      
 
      for(previous_blendjob = *head;
 
	  previous_blendjob
 
	    && previous_blendjob->next != bj; /*< stop on the blendjob that comes before bj */
 
	  previous_blendjob = previous_blendjob->next)
 
	/* all of the action is in the definition of the for loop itself */;
 
  
 
      /*
 
	This removes references to bj from the linked list. I.E., we now skip bj when iterating through the list
 
       */
 
      previous_blendjob->next = bj->next;
 
    }
 
  
 
  /* 
 
     @lordofwar: the magic deallocation of memory ;-)
 
   */
 
  free(bj);
 
}
 

	
 

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

	
 
int main(int argc, char *argv[])
 
{
 
//// Begin Getopt Sample, EDITME!Sometime
 
		   int aflag = 0;
 
	       int bflag = 0;
 
	       char *cvalue = NULL;
 
	       int index;
 
	       int c;
 

	
 
	       opterr = 0;
 

	
 
	       while ((c = getopt (argc, argv, "abc:")) != -1)
 
	         switch (c)
0 comments (0 inline, 0 general)