mems_event.c

来自「disksim是一个非常优秀的磁盘仿真工具」· C语言 代码 · 共 1,946 行 · 第 1/4 页

C
1,946 行
字号
      int lastblock;      mems_reqinfo_t *reqinfo;      int started_mapping = FALSE;      tipsector_coord_set_t tmp_coordset_up, tmp_coordset_dn;      struct tipset tmp_tipset;      tipsector_coord_set_t first_coordset_up, first_coordset_dn;      struct tipset first_tipset;      mems_extent_t *extent_ptr;            reqinfo = sled->active_request->mems_reqinfo;      #ifdef VERBOSE_EVENTLOOP      printf("MEMS_SLED_SCHEDULE:: before\n");      print_extent_info(reqinfo);#endif            // go through the extents and assign the next blocks      extent_ptr = reqinfo->extents;            for (extent = 0; extent < reqinfo->num_extents; extent++)	{	  if (extent_ptr->media_done == TRUE)	    {	      extent_ptr = extent_ptr->next;	      continue;	    }	  if (extent_ptr->completed_block_media == -1)	    {	      extent_ptr->next_block_start = extent_ptr->firstblock;	    }	  else	    {	      extent_ptr->next_block_start = extent_ptr->completed_block_media + 1;	    }	  	  // set the first lbn's position - this is what we compare to the	  // rest of the way.	  if (started_mapping == FALSE)	    {	      mems_lbn_to_position(extent_ptr->next_block_start,				   sled,				   &sled->coordset_up,				   &sled->coordset_dn,				   &sled->tipset,				   NULL, NULL, NULL);	      // this is a hack since the first block gets mapped twice	      sled->tipset.num_tips = 0;  	      started_mapping = TRUE;	    }	  // find the position for this extent's next block	  mems_lbn_to_position(extent_ptr->next_block_start,			       sled,			       &extent_ptr->coordset_up,			       &extent_ptr->coordset_dn,			       &extent_ptr->tipset,			       NULL, NULL, NULL);	  	  if (mems_equal_coord_sets(&sled->coordset_up,				    &extent_ptr->coordset_up) == FALSE)	    {	      extent_ptr->next_block_start = -1;	      extent_ptr->next_block_end = -1;	      extent_ptr = extent_ptr->next;	      continue;	    }	  	  if (sled->tipset.num_tips + extent_ptr->tipset.num_tips	      >	      sled->tips_simultaneous)	    {	      extent_ptr->next_block_start = -1;	      extent_ptr->next_block_end = -1;	      extent_ptr = extent_ptr->next;	      continue;	    }	  	  sled->tipset.num_tips += extent_ptr->tipset.num_tips;	  extent_ptr->next_block_end = extent_ptr->next_block_start;	  	  // go through this extent's blocks and find the last that can be	  // accessed in parallel	  for (block = extent_ptr->next_block_end + 1;	       block <= extent_ptr->lastblock;	       block++)	    {	      // if this is a write and we haven't transferred this block over the bus,	      // it can't be written yet.	      if (!(sled->active_request->flags & READ) &&		  (block > extent_ptr->completed_block_bus))		{		  break;		}	      	      mems_lbn_to_position(block,				   sled,				   &tmp_coordset_up,				   &tmp_coordset_dn,				   &tmp_tipset,				   NULL, NULL, NULL);	      	      // if this block's coordset is not equal to first_coordset,	      // then they are in different positions and can't be accessed	      // in parallel and we're done	      if (mems_equal_coord_sets(&sled->coordset_up,					&tmp_coordset_up) == FALSE)		{		  break;		}	      	      // if adding this block exceeds the number of available tips,	      // then break.	      if (sled->tipset.num_tips + tmp_tipset.num_tips		  >		  sled->tips_simultaneous)		{		  break;		}		      sled->tipset.num_tips += tmp_tipset.num_tips;	      extent_ptr->next_block_end = block;	    }	  extent_ptr = extent_ptr->next;	}      /* If we've started prefetching, update prefetch info */      /*	if (reqinfo->next_block_end > reqinfo->lastblock) {	sled->prefetch_info->next_block_end = reqinfo->next_block_end;	sled->prefetch_info->next_block_start = reqinfo->lastblock + 1;	reqinfo->next_block_end = reqinfo->lastblock;	}      */    }  else    {      /* No active request, no prefetch information, nothing to do, so	 set the sled to be idle and schedule an event for when it	 should actually spin down. */      if (curr->time < sled->lastreq_comptime + sled->inactive_delay_ms)	{	  // printf("I think it's idle, but not spun down\n");	  sled->lastreq_comptime = curr->time;	  sled->active = MEMS_SLED_IDLE;	  sled->active_request = curr;   /* keep a pointer to this event in case */	  	  curr->type = MEMS_SLED_SCHEDULE;	  curr->time = simtime + sled->inactive_delay_ms;	  addtointq((event *)curr);	  return;	}      else if (curr->time	       >=	       sled->lastreq_comptime + sled->inactive_delay_ms)	{	  /* If enough time has passed, switch sled to inactive state */	  // printf("Now I've spun down.\n");	  sled->lastreq_comptime = curr->time;	  // sled->lastreq_lbn = mems_centermost_lbn(sled);	  	  /* Initialize sled position, velocity at centermost LBN */	  mems_lbn_to_position(mems_centermost_lbn(sled), sled, &sled->coordset_up, 			       &sled->coordset_dn, &sled->tipset, 			       NULL, NULL, NULL);	  mems_coord_t_copy(&sled->coordset_up.servo_start, &sled->pos);	  sled->active = MEMS_SLED_INACTIVE;	  addtoextraq((event *)curr);	  	  /* Spindown/spinup statistics */	  sled->stat.num_spindowns++;	  sled->dev->stat.num_spindowns++;	  	  return;	}    }    // printf("I DID THIS!!\n");#ifdef VERBOSE_EVENTLOOP  printf("MEMS_SLED_SCHEDULE:: after\n");  print_extent_info(sled->active_request->mems_reqinfo);#endif  curr->type = MEMS_SLED_SEEK;  addtointq((event *)curr);}voidmems_update_seek_stats(double seek_time,		       double x_seek_time,		       double y_seek_time,		       double turnaround_time,		       int num_turnarounds,		       mems_sled_t *sled){  if (sled->active_request)    {      mems_reqinfo_t *reqinfo = sled->active_request->mems_reqinfo;            reqinfo->subtrack_access_num++;      // printf("subtrack_access_num = %d\n", reqinfo->subtrack_access_num);      reqinfo->subtrack_access_tips += sled->tip_sectors_per_lbn *	(reqinfo->next_block_end - reqinfo->next_block_start + 1);            if (reqinfo->firstseek)	{	  stat_update(&sled->dev->stat.seek_time, seek_time);	  stat_update(&sled->stat.seek_time, seek_time);	  stat_update(&sled->dev->stat.x_seek_time, x_seek_time);	  stat_update(&sled->stat.x_seek_time, x_seek_time);	  stat_update(&sled->dev->stat.y_seek_time, y_seek_time);	  stat_update(&sled->stat.y_seek_time, y_seek_time);	  	  stat_update(&sled->dev->stat.turnaround_time, turnaround_time);	  stat_update(&sled->stat.turnaround_time, turnaround_time);	  stat_update(&sled->dev->stat.turnaround_number, num_turnarounds);	  stat_update(&sled->stat.turnaround_number, num_turnarounds);	  	  sled->dev->stat.num_initial_turnarounds += num_turnarounds;	  sled->stat.num_initial_turnarounds += num_turnarounds;	  	  /*	    fprintf(stderr, "MEMS_SLED_SEEK - added %d initial turnarounds\n",	    num_turnarounds);	    fprintf(stderr, "MEMS_SLED_SEEK - num_initial_turnarounds = %d\n",	    sled->dev->stat.num_initial_turnarounds);	  */	  	  reqinfo->firstseek = 0;	}      else if (num_turnarounds != 0)	{  /*  we must be streaming  */	  stat_update(&sled->dev->stat.stream_turnaround_time, turnaround_time);	  stat_update(&sled->stat.stream_turnaround_time, turnaround_time);	  stat_update(&sled->dev->stat.stream_turnaround_number, num_turnarounds);	  stat_update(&sled->stat.stream_turnaround_number, num_turnarounds);	  	  sled->dev->stat.num_stream_turnarounds += num_turnarounds;	  sled->stat.num_stream_turnarounds += num_turnarounds;	  /*	    fprintf(stderr, "MEMS_SLED_SEEK - added %d streaming turnarounds\n",	    num_turnarounds);	    fprintf(stderr, "MEMS_SLED_SEEK - num_streaming_turnarounds = %d\n",	    sled->dev->stat.num_streaming_turnarounds);	  */	}    }}voidmems_sled_seek(ioreq_event *curr){  mems_sled_t *sled = curr->mems_sled;  enum direction_enum { UP, DOWN } direction;  double time_up, x_up, y_up, to_up;  double time_dn = 0.0;  double x_dn, y_dn, to_dn;  int num_up, num_dn;  double timedelta;#ifdef VERBOSE_EVENTLOOP  printf("MEMS_SLED_SEEK: %f\n", simtime);  fflush(stdout);#endif  time_up = mems_seek_time_seekcache(sled, &sled->pos, 				     &sled->coordset_up.servo_start, 				     &x_up, &y_up, &to_up, &num_up);  /*    time_up = mems_seek_time(sled, &sled->pos,     &sled->coordset_up.servo_start,     &x_up, &y_up, &to_up, &num_up);  */  /* This call can be expensive, so don't do it if unnecessary */  if (sled->bidirectional_access)    {      time_dn = mems_seek_time_seekcache(sled, &sled->pos, 					 &sled->coordset_dn.servo_start, 					 &x_dn, &y_dn, &to_dn, &num_dn);    }    if (!sled->bidirectional_access)    {      direction = UP;    }  else if (time_up < time_dn)    {      direction = UP;    }  else    {      direction = DOWN;    }    /* FIXME: If the request spans multiple adjacent sectors,   * we want zero seek time between the adjacent sectors.  To ensure   * this, we want the _initial_ seek to go to the position farthest   * from the _second_ sector, so when it finishes accessing the   * first sector it can just keep on trucking in the same   * direction.  (Am I making sense here?) */    if (direction == UP)    {      timedelta = time_up;      mems_tipsector_coord_set_t_copy(&sled->coordset_up, &sled->coordset);      mems_update_seek_stats(time_up, x_up, y_up, to_up, num_up, sled);        }  else    {      timedelta = time_dn;      mems_tipsector_coord_set_t_copy(&sled->coordset_dn, &sled->coordset);      mems_update_seek_stats(time_dn, x_dn, y_dn, to_dn, num_dn, sled);        }  /*    fprintf(stderr, "MEMS_SLED_SEEK - timedelta = %f\n", timedelta);  */  curr->type = MEMS_SLED_SERVO;  curr->time += timedelta;  addtointq((event *)curr);  mems_commit_move(sled, &sled->coordset.servo_start);    /* Energy statistics */  mems_energy_update_sled_seek(sled, timedelta);}voidmems_sled_servo(ioreq_event *curr){  mems_sled_t *sled = curr->mems_sled;  double timedelta;#ifdef VERBOSE_EVENTLOOP  printf("MEMS_SLED_SERVO: %f\n", simtime);  fflush(stdout);  /*    fprintf(stderr, "x_pos = %d, y_pos = %d\n",    sled->pos.x_pos, sled->pos.y_pos);      fprintf(stderr, "tip_start = %d, tip_end = %d\n",    sled->tipset.tip_start, sled->tipset.tip_end);  */#endif  /* Calculate servo access time */  timedelta = mems_media_access_time(sled, &sled->pos, 				     &sled->coordset.tipsector_start);    /* Only access data if there is an active request or prefetching */  if (sled->active_request || sled->prefetch_info)    {      curr->type = MEMS_SLED_DATA;    }  else    {      curr->type = MEMS_SLED_SCHEDULE;    }  /* Weird special case:  If this is a write request, and we   * haven't busxfered ANY of the blocks we're trying to write, then   * we should return to MEMS_SLED_SCHEDULE immediately.  It   * was easier to put in this small hack than do a lot of   * hacking above.    *   * Note I don't expect this to ever actually happen, but   * we're building a quality simulator here. */  if (sled->active_request)    {      mems_reqinfo_t *reqinfo = sled->active_request->mems_reqinfo;      mems_extent_t *extent_ptr = reqinfo->extents;      int i;      int data_to_write = TRUE;            for (i = 0; i < reqinfo->num_extents; i++)	{	  if (!(extent_ptr->request->flags & READ) &&	      (extent_ptr->next_block_start > extent_ptr->completed_block_bus))	    {	      data_to_write = FALSE;	    }	  else	    {	      data_to_write = TRUE;	    }	  extent_ptr = extent_ptr->next;	}      if (data_to_write == FALSE)	{	  curr->type = MEMS_SLED_SCHEDULE;	  /* Turn only one tip on during the access (minimize energy) */	  // sled->tipset.tip_end = sled->tipset.tip_start;	  sled->tipset.num_tips = 1;	  	  // fprintf(stderr,"Woah! The weird special case is here!\n");	}    }    curr->time += timedelta;  addtointq((event *)curr);  mems_commit_move(sled, &sled->coordset.tipsector_start);    /* Energy statistics */  mems_energy_update_sled_servo(sled, timedelta);}voidmems_sled_data(ioreq_event *curr){  mems_sled_t *sled = curr->mems_sled;  double timedelta;#ifdef VERBOSE_EVENTLOOP  printf("MEMS_SLED_DATA: %f\n", simtime);  /*  fprintf(stderr, "x_pos = %d, y_pos = %d\n",	  sled->pos.x_pos, sled->pos.y_pos);    fprintf(stderr, "tip_start = %d, tip_end = %d\n",	  sled->tipset.tip_start, sled->tipset.tip_end);  */#endif    /* Calculate data access time */  timedelta = mems_media_access_time(sled, &sled->pos, 				     &sled->coordset.tipsector_end);    curr->type = MEMS_SLED_UPDATE;  curr->time += timedelta;  addtointq((event *)curr);  mems_commit_move(sled, &sled->coordset.tipsector_end);    /* Energy statistics */  mems_energy_update_sled_data(sled, timedelta);}voidmems_sled_update(ioreq_event *curr){  mems_sled_t *sled = curr->mems_sled;#ifdef VERBOSE_EVENTLOOP  printf("MEMS_SLED_UPDATE: %f\n", simtime);  fflush(stdout);#endif    if (sled->active_request)    {      mems_reqinfo_t *reqinfo = sled->active_request->mems_reqinfo;      int extent;      mems_extent_t *extent_ptr;      mems_extent_t *next_extent;      int all_media_done = TRUE;      int num_initial_extents = reqinfo->num_extents;#ifdef VERBOSE_EVENTLOOP      printf("MEMS_SLED_UPDATE:: before\n");      print_extent_info(reqinfo);#endif            extent_ptr = reqinfo->extents;            for (extent = 0; extent < num_initial_extents; extent++)	{	  double latency = 0.0;	  next_extent = extent_ptr->next;	  extent_ptr->completed_block_media = extent_ptr->next_block_end;	  /* BUFFER - insert the blocks into the buffer */	  if (extent_ptr->request->flags & READ) {	    mems_buffer_insert(extent_ptr->next_block_start, 			       extent_ptr->next_block_end, sled->dev);      	  }	  	  // if this extent is a read and the bus is not pending,	  // then initiate a bus access - but only one!	  if ((extent_ptr->request->flags & READ) &&	      (reqinfo->bus_pending == FALSE))	    {	      ioreq_event *busreq = ioreq_copy(sled->active_request);	      busreq->type = MEMS_BUS_INITIATE;	      busreq->time = simtime;	      addtointq((event *)busreq);	      reqinfo->bus_pending = TRUE;	      assert(extent_ptr->bus_done == FALSE);	    }	  	  if (extent_ptr->completed_block_media >= extent_ptr->lastblock)	    {	      extent_ptr->media_done = TRUE;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?