summaryrefslogtreecommitdiff
path: root/sys/dev/raidframe/rf_geometry.c
blob: 15da7cdda8ee1300efa7aab26a41a7542ffaa964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/*	$OpenBSD: rf_geometry.c,v 1.1 1999/01/11 14:29:24 niklas Exp $	*/
/*	$NetBSD: rf_geometry.c,v 1.1 1998/11/13 04:20:30 oster Exp $	*/
/*
 * Copyright (c) 1995 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Mark Holland
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

/* 
 * Changes:
 *	10/24/91  Changes to support disk bus contention model
 *	(MCH)	  1.  Added media_done_time param to Access_time()
 *
 *	08/18/92  Geometry routines have been modified to support zone-bit 
 *		  recording.
 *	(AS)	  1.  Each routine which originally referenced the variable
 *		      'disk->geom->sectors_per_track' has been modified, 
 *		      since the number of sectors per track varies on disks
 *		      with zone-bit recording.
 */

/* :  
 * Log: rf_geometry.c,v 
 * Revision 1.18  1996/08/11 00:40:57  jimz
 * fix up broken comment
 *
 * Revision 1.17  1996/07/28  20:31:39  jimz
 * i386netbsd port
 * true/false fixup
 *
 * Revision 1.16  1996/07/18  22:57:14  jimz
 * port simulator to AIX
 *
 * Revision 1.15  1996/06/07  21:33:04  jimz
 * begin using consistent types for sector numbers,
 * stripe numbers, row+col numbers, recon unit numbers
 *
 * Revision 1.14  1996/06/05  18:06:02  jimz
 * Major code cleanup. The Great Renaming is now done.
 * Better modularity. Better typing. Fixed a bunch of
 * synchronization bugs. Made a lot of global stuff
 * per-desc or per-array. Removed dead code.
 *
 * Revision 1.13  1996/05/30  23:22:16  jimz
 * bugfixes of serialization, timing problems
 * more cleanup
 *
 * Revision 1.12  1996/05/30  11:29:41  jimz
 * Numerous bug fixes. Stripe lock release code disagreed with the taking code
 * about when stripes should be locked (I made it consistent: no parity, no lock)
 * There was a lot of extra serialization of I/Os which I've removed- a lot of
 * it was to calculate values for the cache code, which is no longer with us.
 * More types, function, macro cleanup. Added code to properly quiesce the array
 * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
 * before. Fixed memory allocation, freeing bugs.
 *
 * Revision 1.11  1996/05/24  22:17:04  jimz
 * continue code + namespace cleanup
 * typed a bunch of flags
 *
 * Revision 1.10  1996/05/23  00:33:23  jimz
 * code cleanup: move all debug decls to rf_options.c, all extern
 * debug decls to rf_options.h, all debug vars preceded by rf_
 *
 * Revision 1.9  1996/05/18  19:51:34  jimz
 * major code cleanup- fix syntax, make some types consistent,
 * add prototypes, clean out dead code, et cetera
 *
 * Revision 1.8  1995/12/12  18:10:06  jimz
 * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
 * fix 80-column brain damage in comments
 *
 * Revision 1.7  1995/12/01  18:29:34  root
 * added copyright info
 *
 */

#include "rf_types.h"
#include "rf_geometry.h"
#include "rf_raid.h"
#include "rf_general.h"
#include "rf_debugMem.h"

#define DISK_DB "disk_db"
#define DISK_NAME "HP2247"

#define ABS_DIFF(a,b) ( ((a)>(b)) ? ((a)-(b)) : ((b)-(a)) )

static RF_GeometryList_t *geom_list = (RF_GeometryList_t *) NULL;

RF_TICS_t rf_globalSpinup = 1.5;

#define NM_LGTH 80
#define NM_PATN " %80s"

static RF_GeometryList_t *Fetch_geometry_db(FILE *fd);
static void Format_disk(RF_DiskState_t *disk, long sectors_per_block);
static long Find_cyl(RF_SectorNum_t block, RF_DiskState_t *disk);
static long Find_track(RF_SectorNum_t block, RF_DiskState_t *disk);
static long Find_phys_sector(RF_SectorNum_t block, RF_DiskState_t *disk);
static RF_TICS_t Delay_to(RF_TICS_t cur_time, RF_SectorNum_t block,
	RF_DiskState_t *disk);
static RF_TICS_t Seek_time(long to_cyl, long to_track, long from_cyl,
	long from_track, RF_DiskState_t *disk);
static RF_TICS_t Seek(RF_TICS_t cur_time, RF_SectorNum_t block,
	RF_DiskState_t *disk, long update);
static RF_TICS_t Rotate(RF_TICS_t cur_time, RF_SectorNum_t block,
	RF_DiskState_t *disk, long update);
static RF_TICS_t Seek_Rotate(RF_TICS_t cur_time, RF_SectorNum_t block,
	RF_DiskState_t *disk, long update);
static RF_TICS_t GAP(long sec_per_track, RF_DiskState_t *disk);
static RF_TICS_t Block_access_time(RF_TICS_t cur_time, RF_SectorNum_t block,
	RF_SectorCount_t numblocks, RF_DiskState_t *disk, long update);
static void Zero_stats(RF_DiskState_t *disk);
static RF_TICS_t Update_stats(RF_TICS_t cur_time, RF_TICS_t seek, RF_TICS_t rotate,
	RF_TICS_t transfer, RF_DiskState_t *disk);
static void rf_DiskParam(long numCyls, RF_TICS_t minSeek, RF_TICS_t avgSeek, RF_TICS_t maxSeek,
	RF_TICS_t *a, RF_TICS_t *b, RF_TICS_t *c);

static RF_GeometryList_t *Fetch_geometry_db(fd)
  FILE  *fd;
{
  long ret, lineno;
  char name[NM_LGTH], title[20];
  RF_GeometryList_t * list = (RF_GeometryList_t *) NULL,
  ** next_ptr = & list;
  
  if( RF_MAX_DISKNAME_LEN<NM_LGTH ) RF_PANIC();
  lineno = 0;
  while( (ret = fscanf( fd, " %20s", title )) != EOF ) {
    float tmp_f1, tmp_f2, tmp_f3, tmp_f4;
    float tmp_f5=0.0;
    float tmp_f6=0.0; 
    RF_Geometry_t *g;
    long i, x, y, z, num_cylinders;
    RF_ZoneList_t ** znext_ptr;

    if( ret == 1 && strncmp( "enddisk", title, 8 ) == 0 ) break;

    RF_Calloc(*next_ptr, 1, sizeof(RF_GeometryList_t), (RF_GeometryList_t *));
    (*next_ptr)->next = (RF_GeometryList_t *) NULL;
    RF_Calloc(g, 1, sizeof(RF_Geometry_t), (RF_Geometry_t *));
    (*next_ptr)->disk = g;
    next_ptr = &( (*next_ptr)->next ); /*prep for next iteration */
    lineno++;
    if (fscanf( fd, NM_PATN, name ) != 1) {
      fprintf(stderr,"Disk DB Error: Can't get disk name from disk db\n");
      fprintf(stderr,"lineno=%d\n", lineno);
      fprintf(stderr,"name=\"%s\"\n", name);
      exit(1);
    }
    lineno++;
    if ( (fscanf(fd, " tracks per cylinder %ld", &(g->tracks_per_cyl)) != 1) || g->tracks_per_cyl <= 0) {
		    fprintf(stderr,"Disk DB Error: Missing or invalid tracks/cyl for disk %s\n", name); exit(1);
		  }
    lineno++;
    if ( (fscanf(fd, " number of disk zones %ld", &(g->num_zones)) != 1) || g->num_zones <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid number of zones for disk %s\n", name); exit(1);
    }
    
    
    
    /* This section of code creates the linked list which
       contains the disk's zone information. */
    g->zbr_data = (RF_ZoneList_t *) NULL;
    znext_ptr = &(g->zbr_data);
    num_cylinders = 0;
    
    /* This for-loop reads in the cylinder count, the sectors
       per track, and track skew for each zone on the disk. */
    for (i=1; i <= g->num_zones; i++) {
      lineno++;
      if ( (fscanf(fd, " number of cylinders in zone %ld", &x) != 1) || x < 1) {
	fprintf(stderr,"Disk DB Error: Zone %ld: Missing or invalid cyls/zone for disk %s\n", i, name); exit(1);
      }
      lineno++;
      if ( (fscanf(fd, " sectors per track in zone %ld", &y) != 1) || y < 1 ) {
	fprintf(stderr,"Disk DB Error: Zone %ld: Missing or invalid sectors/track for disk %s\n", i, name); exit(1);
      }
      lineno++;
      if ( (fscanf(fd, " track skew in zone %ld", &z) != 1) || z < 0 ) {
	fprintf(stderr,"Disk DB Error: Zone %ld: Missing or invalid track skew for disk %s\n",i, name); exit(1);
      }

      RF_Calloc(*znext_ptr, 1, sizeof(RF_ZoneList_t), (RF_ZoneList_t *));
      (*znext_ptr)->next = (RF_ZoneList_t *) NULL;
      (*znext_ptr)->zone.num_cylinders = x;
      (*znext_ptr)->zone.sec_per_track = y;
      (*znext_ptr)->zone.track_skew = z;
      (*znext_ptr)->zone.num_sectors = 
	(*znext_ptr)->zone.num_cylinders *
	  g->tracks_per_cyl *
	    (*znext_ptr)->zone.sec_per_track;
      znext_ptr = &((*znext_ptr)->next);
      num_cylinders = num_cylinders + x;
    } /* End of for-loop */
    
    lineno++;
    if ( (fscanf(fd, " revolution time %f", &tmp_f1) != 1) || tmp_f1 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid revolution time for disk %s\n",name); exit(1);
    }
    lineno++;
    if ( (fscanf(fd, " 1 cylinder seek time %f", &tmp_f2 ) != 1) || tmp_f2 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid 1-cyl seek time for disk %s\n",name); exit(1);
    }
    lineno++;
    if ( (fscanf(fd, " max stroke seek time %f", &tmp_f3) != 1) || tmp_f3 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid max seek time for disk %s\n",name); exit(1);
    }
    lineno++;
    if ( (fscanf(fd, " average seek time %f", &tmp_f4) != 1) || tmp_f4 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid avg seek time for disk %s\n",name); exit(1);
    }
    lineno++;
    if ( (fscanf(fd, " time to sleep %f", &tmp_f5) != 1) || tmp_f4 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid time to sleep for disk %s\n",name); exit(1);
		}
    lineno++;
    if ( (fscanf(fd, " time to spinup %f", &tmp_f6) != 1) || tmp_f4 <= 0) {
      fprintf(stderr,"Disk DB Error: Missing or invalid time to sleep for disk %s\n",name); exit(1);
    }   
    strcpy( g->disk_name, name );
    g->revolution_time = tmp_f1;
    g->seek_one_cyl = tmp_f2;
    g->seek_max_stroke = tmp_f3;
    g->seek_avg = tmp_f4;
    g->time_to_sleep = tmp_f5;
    g->time_to_spinup = tmp_f6;
    /* convert disk specs to seek equation coeff */
    rf_DiskParam( num_cylinders, g->seek_one_cyl,
	      g->seek_avg, g->seek_max_stroke,
	      &g->seek_sqrt_coeff, &g->seek_linear_coeff,
	      &g->seek_constant_coeff );
  }
  return( list );
}

static void Format_disk(disk, sectors_per_block)
  RF_DiskState_t  *disk;
  long             sectors_per_block;
{
  long sector_count = 0;
  RF_ZoneList_t *z;

  if( disk == (RF_DiskState_t *) NULL ) RF_PANIC();
  if( disk->geom == (RF_Geometry_t *) NULL ) RF_PANIC();
  if( sectors_per_block <=0 ) RF_PANIC();
  
  disk->sectors_per_block = sectors_per_block;
  z = disk->geom->zbr_data;
  /* This while-loop visits each disk zone and computes the total
     number of sectors on the disk. */
  while (z != (RF_ZoneList_t *) NULL) {
    sector_count = sector_count + (z->zone.num_cylinders * 
				   disk->geom->tracks_per_cyl * 
				   z->zone.sec_per_track);
    z = z->next;
  }
  
  disk->last_block_index = (sector_count / sectors_per_block) - 1;
}

void rf_InitDisk( disk, disk_db, disk_name, init_cyl, init_track, init_offset, row, col)
  RF_DiskState_t  *disk;
  char            *disk_db;
  char            *disk_name;
  long            init_cyl;
  long            init_track;
  RF_TICS_t       init_offset;
  int             row;
  int             col;
{
  RF_GeometryList_t *gp;
  FILE *f;
  
  RF_ASSERT( disk != (RF_DiskState_t *) NULL );
  
  disk->cur_cyl = init_cyl;
  disk->cur_track = init_track;
  disk->index_offset = init_offset;
  disk->geom = (RF_Geometry_t *) NULL;
  disk->queueFinishTime = 0.0;
  disk->lastBlock = 0;
  disk->row=row;
  disk->col=col;
  Zero_stats(disk);

  if (strncmp(disk_name,"/dev",4 )==0) strcpy(disk_name,"HP2247");
  
  if( geom_list == (RF_GeometryList_t *) NULL ) {
    f = fopen(disk_db,"r");
    if (f == NULL) {
      fprintf(stderr, "ERROR: RAIDframe could not open disk db %s\n", disk_db);
      exit(1);
    }
    geom_list = Fetch_geometry_db( f );
    fclose( f );
  }
  for( gp = geom_list; gp != (RF_GeometryList_t *) NULL; gp = gp->next ) {
    RF_ASSERT( gp->disk != (RF_Geometry_t *) NULL
	   &&  gp->disk->disk_name != (char *) NULL );
    if( strncmp( disk_name, gp->disk->disk_name, RF_MAX_DISKNAME_LEN )
       == 0 ) {
      disk->geom = gp->disk;
      break;
    }
  }
  if( disk->geom == (RF_Geometry_t *) NULL ) {
    fprintf( stderr, "Disk %s not found in database %s\n",
	    disk_name, disk_db );
    exit(1);
  }
  
  Format_disk( disk, 1 );
}

static long Find_cyl( block, disk )
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
{
  RF_ZoneList_t * z;
  long tmp;

  long log_sector = block * disk->sectors_per_block;
  long cylinder = 0;
  z = disk->geom->zbr_data;
  /* This while-loop finds the zone to which log_sector belongs, 
     computes the starting cylinder number of this zone, and 
     computes the sector offset into this zone. */
  while (log_sector >= z->zone.num_sectors) {
    log_sector = log_sector - z->zone.num_sectors;
    cylinder = cylinder + z->zone.num_cylinders;
    z = z->next;
  }
  
  /* The cylinder to which log_sector belongs equals the starting
     cylinder number of its zone plus the cylinder offset into
     the zone. */
  tmp = cylinder + (log_sector / (z->zone.sec_per_track * 
				  disk->geom->tracks_per_cyl));
  
  return( tmp );
}

static long Find_track( block, disk )
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
{
  RF_ZoneList_t * z;
  long tmp;
  
  long log_sector = block * disk->sectors_per_block;
  long track = 0;
  z = disk->geom->zbr_data;
  /* This while-loop finds the zone to which log_sector belongs,
     computes the starting track number of this zone, and computes
     the sector offset into this zone. */
  while (log_sector >= z->zone.num_sectors) {
    log_sector = log_sector - z->zone.num_sectors;
    track = track + (z->zone.num_cylinders * 
		     disk->geom->tracks_per_cyl);
    z = z->next;
  }
  
  /* The track to which log_sector belongs equals the starting
     track number of its zone plus the track offset into the zone,
     modulo the number of tracks per cylinder on the disk. */
  tmp = (track + (log_sector / z->zone.sec_per_track)) % 
    disk->geom->tracks_per_cyl;
  
  return( tmp );
}

/*
 ** The position of a logical sector relative to the index mark on any track
 ** is not simple.  A simple organization would be:
**
** 	track 0	:	0,  1,  2,  3,  ...  N-1
**	track 1 :	N,N+1,N+2,N+3,  ... 2N-1
**			^
**			Index mark just before this point
**
** This is not good because sequential access of sectors N-1 then N
** will require a full revolution in between (because track switch requires
** a couple of sectors to recalibrate from embedded servo).  So frequently
** sequentially numbered sectors are physically skewed so that the next
** accessible sector after N-1 will be N (with a skew of 2)
**
** 	track 0	:	   0,   1,   2,   3,  ...  N-1
**	track 1 :	2N-2,2N-1,   N, N+1,  ... 2N-3
**			^
**			Index mark just before this point
**
** Layout gets even more complex with cylinder boundaries.  Seek time
** is A + B*M where M is the number of cylinders to seek over.  On a sequential
** access that crosses a cylinder boundary, the disk will rotate for
** A+B seconds, then "track skew" sectors (inter-sector gaps actually)
** before it can access another sector, so the cylinder to cylinder skew
** is "track skew" + CEIL( sectors_per_track*(A+B)/revolution_time ).
**
** So if sector 0 is 0 sectors from the index mark on the first track,
** where is sector X relative to the index mark on its track?
**
**	( ( X % sectors_per_track ) 	 basic relative position **
**	+ track_skew * ( X / sectors_per_track )  skewed for each track **   
**	+ CEIL( sectors_per_track*(A+B)/revolution_time )
**		* ( X / sectors_per_cylinder )	 skewed more for each cyl **
**	) % sectors_per_track		 wrapped around in the track **
**
**
*/

static long Find_phys_sector(block, disk)
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
{
  long phys = 0;
  RF_ZoneList_t * z;
  long previous_spt = 1;
  long sector = block * disk->sectors_per_block;
  
  z = disk->geom->zbr_data;
  /* This while-loop finds the zone to which sector belongs,
     and computes the physical sector up to that zone. */
  while (sector >= z->zone.num_sectors) {
    sector = sector - z->zone.num_sectors;
    /* By first multiplying 'phys' by the sectors per track in
       the current zone divided by the sectors per track in the
       previous zone, we convert a given physical sector in one
       zone to an equivalent physical sector in another zone. */
	  phys = ((phys * z->zone.sec_per_track / previous_spt) +
		  (((z->zone.num_sectors - 1) % z->zone.sec_per_track) + 
		   (z->zone.track_skew * z->zone.num_cylinders *
		    disk->geom->tracks_per_cyl) +
		   (long) ceil( (double) z->zone.sec_per_track *
			       (disk->geom->seek_constant_coeff) /
			       disk->geom->revolution_time) *
		   z->zone.num_cylinders)) %
		     z->zone.sec_per_track;
    previous_spt = z->zone.sec_per_track;
    z = z->next;
  }
  
  /* The final physical sector equals the physical sector up to 
     the particular zone, plus the physical sector caused by the
     sector offset into this zone. */
  phys = ((phys * z->zone.sec_per_track / previous_spt) +
	  ((sector % z->zone.sec_per_track) +
	   (z->zone.track_skew * (sector / z->zone.sec_per_track)) +
	   (long) ceil( (RF_TICS_t) z->zone.sec_per_track * 
		       (disk->geom->seek_constant_coeff) /
		       disk->geom->revolution_time) *
	   (sector / (z->zone.sec_per_track * 
		      disk->geom->tracks_per_cyl)))) %
			z->zone.sec_per_track;
  
  
  return( phys );
}

/*
 ** When each disk starts up, its index mark is a fraction (f) of a rotation
 ** ahead from its heads (in the direction of rotation).  The sector 
 ** under its heads is at a fraction f of a rotation from the index
 ** mark.  After T time has past, T/rotation_time revolutions have occured, so
 ** the sector under the heads is at a fraction FRAC(f+T/rotation_time) of a
 ** rotation from the index mark.  If the target block is at physical sector
 ** X relative to its index mark, then it is at fraction (X/sectors_per_track),
 ** so the rotational delay is 
 ** ((X/sectors_per_track)-FRAC(f+T/rotation_time)) * revolution_time
 ** if this is positive, otherwise it is
 ** (1+(X/sectors_per_track)-FRAC(f+T/rotation_time)) * revolution_time
 */

#define FRAC(a) ( (a) - (long) floor(a) )

static RF_TICS_t Delay_to(cur_time, block, disk)
  RF_TICS_t        cur_time;
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
{
  RF_TICS_t tmp;
  RF_ZoneList_t *z;
  
  long sector = block * disk->sectors_per_block;
  z = disk->geom->zbr_data;
  /* This while-loop finds the zone to which sector belongs. */
  while (sector >= z->zone.num_sectors) {
    sector = sector - z->zone.num_sectors;
    z = z->next;
  }
  
  tmp = (
	 (RF_TICS_t) Find_phys_sector(block,disk)/z->zone.sec_per_track
	 - FRAC(disk->index_offset+cur_time/disk->geom->revolution_time)
	 ) * disk->geom->revolution_time;
  if( tmp < 0 ) tmp += disk->geom->revolution_time;
  if( tmp < 0 ) RF_PANIC();
  return( tmp );
}

/* Hmmm...they seem to be computing the head switch time as
 * equal to the track skew penalty.  Is this an approximation?
 * (MCH)
 */
static RF_TICS_t Seek_time( to_cyl, to_track, from_cyl, from_track, disk )
  long             to_cyl;
  long             to_track;
  long             from_cyl;
  long             from_track;
  RF_DiskState_t  *disk;
{
  long cyls = ABS_DIFF( from_cyl, to_cyl ) - 1;
  RF_TICS_t seek = 0.0;
  RF_ZoneList_t * z;
  
  /* printf("Seek_time:  from_cyl %ld, to_cyl %ld, from_trk %ld, to_trk %ld\n",from_cyl, to_cyl, from_track, to_track); */
  if( from_cyl != to_cyl ) {
    z = disk->geom->zbr_data;
    /* This while-loop finds the zone to which to_cyl belongs. */
    while (to_cyl >= z->zone.num_cylinders) {
      to_cyl = to_cyl - z->zone.num_cylinders;
      z = z->next;
    }
    
    seek = disk->geom->seek_constant_coeff 
      + disk->geom->seek_linear_coeff * cyls
	+ disk->geom->seek_sqrt_coeff * sqrt( (double) cyls ) 
	  + z->zone.track_skew * disk->geom->revolution_time /
	    z->zone.sec_per_track;
    
  } else if( from_track != to_track ) {
    /* from_track and to_track must lie in the same zone. */
    z = disk->geom->zbr_data;
    /* This while-loop finds the zone to which from_cyl belongs. */
    while (from_cyl >= z->zone.num_cylinders) {
      from_cyl = from_cyl - z->zone.num_cylinders;
      z = z->next;
    } 
    
    seek = z->zone.track_skew
      * disk->geom->revolution_time
	/ z->zone.sec_per_track;
  }
  return( seek );
}

static RF_TICS_t Seek(cur_time, block, disk, update)
  RF_TICS_t        cur_time;
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
  long             update;
{
  long cur_cyl, cur_track;
  /* 
   ** current location is derived from the time,
   ** current track and current cylinder
   ** 
   ** update current location as you go
   */
  
  RF_ASSERT( block <= disk->last_block_index );
  cur_cyl = disk->cur_cyl;
  cur_track = disk->cur_track;
  if (update) {
    disk->cur_cyl = Find_cyl( block, disk );
    disk->cur_track = Find_track( block, disk );
  }
  return( Seek_time( disk->cur_cyl, disk->cur_track,
		    cur_cyl, cur_track, disk ) );
}

static RF_TICS_t Rotate(cur_time, block, disk, update)
  RF_TICS_t        cur_time;
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
  long             update;
{
  /* 
   ** current location is derived from the time,
   ** current track and current cylinder
   ** 
   ** block the process until at the appropriate block
   ** updating current location as you go
   */
  
  RF_ASSERT( block <= disk->last_block_index );
  return( Delay_to( cur_time, block, disk ) );
}

static RF_TICS_t Seek_Rotate(cur_time, block, disk, update)
  RF_TICS_t        cur_time;
  RF_SectorNum_t   block;
  RF_DiskState_t  *disk;
  long             update;
{
  RF_TICS_t seek, delay;

  RF_ASSERT( block <= disk->last_block_index );
  seek = Seek( cur_time, block, disk, update );
  delay =  seek + Rotate( cur_time+seek, block, disk, update );
  return( delay );
}

static RF_TICS_t GAP(sec_per_track, disk)
  long             sec_per_track;
  RF_DiskState_t  *disk;
{
  RF_TICS_t tmp = (disk->geom->revolution_time/(100*sec_per_track));
  return (tmp);
}

RF_TICS_t Block_access_time(cur_time, block, numblocks, disk, update)
  RF_TICS_t          cur_time;
  RF_SectorNum_t     block;
  RF_SectorCount_t   numblocks;
  RF_DiskState_t    *disk;
  long               update;
{
  RF_TICS_t delay = 0;
  long cur = block, end = block + numblocks;
  long sector, tmp;
  RF_ZoneList_t * z;
  /*
   ** this is the same as Seek_Rotate by merit of the mapping
   ** except that the access ends before the gap to the next block
   */
  RF_ASSERT( numblocks > 0 && end-1 <= disk->last_block_index );
  
  while( cur < end ) {
    sector = cur * disk->sectors_per_block;
    z = disk->geom->zbr_data;
    /* This while-loop finds the zone to which sector belongs. */
    while (sector >= z->zone.num_sectors) {
      sector = sector - z->zone.num_sectors;
      z = z->next;
    }
    
    tmp = RF_MIN( end - cur, z->zone.sec_per_track
	      - cur % z->zone.sec_per_track );
    delay += tmp * disk->geom->revolution_time /
      z->zone.sec_per_track - 
	GAP(z->zone.sec_per_track, disk);
    cur += tmp;
    if( cur != end )
      delay += Seek_Rotate( cur_time+delay, cur, disk, update );
  }
  return( delay );
}

static void Zero_stats(disk)
  RF_DiskState_t  *disk;
{
  char traceFileName[64];
  disk->stats.num_events = 0;
  disk->stats.seek_sum = 0;
  disk->stats.seekSq_sum = 0;
  disk->stats.rotate_sum = 0;
  disk->stats.rotateSq_sum = 0;
  disk->stats.transfer_sum = 0;
  disk->stats.transferSq_sum = 0;
  disk->stats.access_sum = 0;
  disk->stats.accessSq_sum = 0;
  disk->stats.sleep_sum=0;
  disk->stats.idle_sum=0;
  disk->stats.rw_sum=0;
  disk->stats.spinup_sum=0;
  disk->stats.last_acc=0;
  if (rf_diskTrace){
    sprintf (traceFileName,"rf_diskTracer%dc%d\0",disk->row,disk->col);
    if ( (disk->traceFile= fopen(traceFileName, "w")) == NULL) {
      perror(traceFileName); RF_PANIC();}
  }
}

static RF_TICS_t Update_stats(cur_time, seek, rotate, transfer, disk)
  RF_TICS_t        cur_time;
  RF_TICS_t        seek;
  RF_TICS_t        rotate;
  RF_TICS_t        transfer;
  RF_DiskState_t  *disk;
{
  RF_TICS_t spinup=0;
  RF_TICS_t sleep=0;
  RF_TICS_t idle=0;
  
  disk->stats.num_events++;
  disk->stats.seek_sum += seek;
  disk->stats.seekSq_sum += seek*seek;
  disk->stats.rotate_sum += rotate;
  disk->stats.rotateSq_sum += rotate*rotate;
  disk->stats.transfer_sum += transfer;
  disk->stats.transferSq_sum += transfer*transfer;
  disk->stats.access_sum += seek+rotate+transfer;
  disk->stats.accessSq_sum +=
    (seek+rotate+transfer)*(seek+rotate+transfer);

/*   ASSERT (cur_time - disk->stats.last_acc >= 0);  */

  if (cur_time-disk->stats.last_acc>disk->geom->time_to_sleep){
    idle=disk->geom->time_to_sleep;  
    
    sleep = cur_time - disk->stats.last_acc - idle;
    spinup=disk->geom->time_to_spinup;
    rf_globalSpinup = spinup;
  }

  else{
    idle=cur_time - disk->stats.last_acc;
  }
  
  
  disk->stats.sleep_sum+=sleep;
  disk->stats.idle_sum+=idle;
  disk->stats.rw_sum+=seek+rotate+transfer;
  disk->stats.spinup_sum+=spinup;

  if (rf_diskTrace){
    fprintf(disk->traceFile,"%g %g\n",disk->stats.last_acc,2.0);
    fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle),2.0);
    if (sleep){
      fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle),1.0);
      fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle+sleep),1.0);
    }
     
    if (spinup){
      fprintf(disk->traceFile,"%g %g\n",(cur_time),4.0);
      fprintf(disk->traceFile,"%g %g\n",(cur_time+spinup),4.0);
    }
 
    fprintf(disk->traceFile,"%g %g\n",(cur_time+spinup),3.0);
    fprintf(disk->traceFile,"%g %g\n",(cur_time+spinup+seek+rotate+transfer),3.0);
    
      
  }

  disk->stats.last_acc=cur_time+spinup+seek+rotate+transfer; 
  
  return(spinup);
}


void rf_StopStats(disk, cur_time)
  RF_DiskState_t  *disk;
  RF_TICS_t        cur_time;
{

	RF_TICS_t sleep=0;
	RF_TICS_t idle=0;

	if (cur_time - disk->stats.last_acc > disk->geom->time_to_sleep){
		
		sleep = cur_time - disk->stats.last_acc-disk->geom->time_to_sleep;
		idle = disk->geom->time_to_sleep;
			
	}

	  

	else{
		idle=cur_time - disk->stats.last_acc;
	}

	disk->stats.sleep_sum+=sleep;
	disk->stats.idle_sum+=idle;

	if (rf_diskTrace){
	  fprintf(disk->traceFile,"%g %g\n",disk->stats.last_acc,2.0);
	  fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle),2.0);
	  if (sleep){
	      fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle),1.0);
	      fprintf(disk->traceFile,"%g %g\n",(disk->stats.last_acc+idle+sleep),1.0);
	    }
	  fclose(disk->traceFile);
	  }
}

/* Sometimes num_events is zero because the disk was failed at the start
 * of the simulation and never replaced.  This causes a crash on some
 * architectures, which is why we have the conditional.
 */
void rf_Report_stats(
  RF_DiskState_t  *disk,
  long            *numEventsPtr,
  RF_TICS_t       *avgSeekPtr,
  RF_TICS_t       *avgRotatePtr,
  RF_TICS_t       *avgTransferPtr,
  RF_TICS_t       *avgAccessPtr,
  RF_TICS_t       *SleepPtr,
  RF_TICS_t       *IdlePtr,
  RF_TICS_t       *RwPtr,
  RF_TICS_t       *SpinupPtr)
{
    *numEventsPtr = disk->stats.num_events;
    if (disk->stats.num_events) {
	*avgSeekPtr = disk->stats.seek_sum / disk->stats.num_events;
	*avgRotatePtr = disk->stats.rotate_sum / disk->stats.num_events;
	*avgTransferPtr = disk->stats.transfer_sum / disk->stats.num_events;
	*avgAccessPtr = disk->stats.access_sum / disk->stats.num_events;
    } else {
	*avgSeekPtr = 0;
	*avgRotatePtr = 0;
	*avgTransferPtr = 0;
	*avgAccessPtr = 0;
    }
    *SleepPtr = disk->stats.sleep_sum;
    *IdlePtr = disk->stats.idle_sum;
    *RwPtr = disk->stats.rw_sum ;
    *SpinupPtr = disk->stats.spinup_sum ;
}

int rf_Access_time( access_time, cur_time, block, numblocks, disk, media_done_time, update )
  RF_TICS_t         *access_time;
  RF_TICS_t          cur_time;
  RF_SectorNum_t     block;
  RF_SectorCount_t   numblocks;
  RF_DiskState_t    *disk;
  RF_TICS_t         *media_done_time;
  long               update; /* 1 => update disk state, 0 => don't */
{
	/*
	 * first move to the start of the data, then sweep to the end
	 */
	RF_TICS_t spinup=0;
	RF_TICS_t seek = Seek( cur_time, block, disk, update );
	RF_TICS_t rotate =  Rotate( cur_time+seek, block, disk, update );
	RF_TICS_t transfer = Block_access_time( cur_time+seek+rotate, block,
				numblocks, disk, update );

	if (update) spinup=Update_stats(cur_time, seek, rotate, transfer, disk );
	*media_done_time = seek+rotate+transfer;
	*access_time =( seek+rotate+transfer+spinup);
	return(0);
}

/* added to take into account the fact that maping code acounts for the disk label */

void rf_GeometryDoReadCapacity(disk, numBlocks, blockSize)
  RF_DiskState_t    *disk;
  RF_SectorCount_t  *numBlocks;
  int               *blockSize;
{
	*numBlocks= (disk->last_block_index + 1 )-rf_protectedSectors;	
	
	*blockSize= (disk->sectors_per_block*512 );
	
	/* in bytes */
}


/* END GEOMETRY ROUTINES **********************************************/


static void rf_DiskParam(numCyls, minSeek, avgSeek, maxSeek, a, b, c)
  long        numCyls;
  RF_TICS_t   minSeek;
  RF_TICS_t   avgSeek;
  RF_TICS_t   maxSeek;
  RF_TICS_t  *a;
  RF_TICS_t  *b;
  RF_TICS_t  *c;
{
    if (minSeek == avgSeek && minSeek == maxSeek) {
	*a = 0.0; *b = 0.0; *c = minSeek;
    } else {
	*a = ( 15 * avgSeek - 10 * minSeek - 5 * maxSeek ) / ( 3 * sqrt( (double) numCyls ));
	*b = ( 7 * minSeek + 8 * maxSeek - 15 * avgSeek ) / ( 3 * numCyls );
	*c = minSeek;
    }
}