《操作系统》的实验代码。
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

119 Zeilen
4.9 KiB

  1. This simulator, paging-policy.py, allows you to play around with different
  2. page-replacement policies. For example, let's examine how LRU performs with a
  3. series of page references with a cache of size 3:
  4. 0 1 2 0 1 3 0 3 1 2 1
  5. To do so, run the simulator as follows:
  6. prompt> ./paging-policy.py --addresses=0,1,2,0,1,3,0,3,1,2,1
  7. --policy=LRU --cachesize=3 -c
  8. And what you would see is:
  9. ARG addresses 0,1,2,0,1,3,0,3,1,2,1
  10. ARG numaddrs 10
  11. ARG policy LRU
  12. ARG cachesize 3
  13. ARG maxpage 10
  14. ARG seed 0
  15. Solving...
  16. Access: 0 MISS LRU-> [br 0]<-MRU Replace:- [br Hits:0 Misses:1]
  17. Access: 1 MISS LRU-> [br 0, 1]<-MRU Replace:- [br Hits:0 Misses:2]
  18. Access: 2 MISS LRU->[br 0, 1, 2]<-MRU Replace:- [br Hits:0 Misses:3]
  19. Access: 0 HIT LRU->[br 1, 2, 0]<-MRU Replace:- [br Hits:1 Misses:3]
  20. Access: 1 HIT LRU->[br 2, 0, 1]<-MRU Replace:- [br Hits:2 Misses:3]
  21. Access: 3 MISS LRU->[br 0, 1, 3]<-MRU Replace:2 [br Hits:2 Misses:4]
  22. Access: 0 HIT LRU->[br 1, 3, 0]<-MRU Replace:2 [br Hits:3 Misses:4]
  23. Access: 3 HIT LRU->[br 1, 0, 3]<-MRU Replace:2 [br Hits:4 Misses:4]
  24. Access: 1 HIT LRU->[br 0, 3, 1]<-MRU Replace:2 [br Hits:5 Misses:4]
  25. Access: 2 MISS LRU->[br 3, 1, 2]<-MRU Replace:0 [br Hits:5 Misses:5]
  26. Access: 1 HIT LRU->[br 3, 2, 1]<-MRU Replace:0 [br Hits:6 Misses:5]
  27. ]
  28. The complete set of possible arguments for paging-policy is listed on the
  29. following page, and includes a number of options for varying the policy, how
  30. addresses are specified/generated, and other important parameters such as the
  31. size of the cache.
  32. prompt> ./paging-policy.py --help
  33. Usage: paging-policy.py [options]
  34. Options:
  35. -h, --help show this help message and exit
  36. -a ADDRESSES, --addresses=ADDRESSES
  37. a set of comma-separated pages to access;
  38. -1 means randomly generate
  39. -f ADDRESSFILE, --addressfile=ADDRESSFILE
  40. a file with a bunch of addresses in it
  41. -n NUMADDRS, --numaddrs=NUMADDRS
  42. if -a (--addresses) is -1, this is the
  43. number of addrs to generate
  44. -p POLICY, --policy=POLICY
  45. replacement policy: FIFO, LRU, LFU, OPT,
  46. UNOPT, RAND, CLOCK
  47. -b CLOCKBITS, --clockbits=CLOCKBITS
  48. for CLOCK policy, how many clock bits to use
  49. -C CACHESIZE, --cachesize=CACHESIZE
  50. size of the page cache, in pages
  51. -m MAXPAGE, --maxpage=MAXPAGE
  52. if randomly generating page accesses,
  53. this is the max page number
  54. -s SEED, --seed=SEED random number seed
  55. -N, --notrace do not print out a detailed trace
  56. -c, --compute compute answers for me
  57. ]
  58. As usual, "-c" is used to solve a particular problem, whereas without it, the
  59. accesses are just listed (and the program does not tell you whether or not a
  60. particular access is a hit or miss).
  61. To generate a random problem, instead of using "-a/--addresses" to pass in
  62. some page references, you can instead pass in "-n/--numaddrs" as the number of
  63. addresses the program should randomly generate, with "-s/--seed" used to
  64. specify a different random seed. For example:
  65. prompt> ./paging-policy.py -s 10 -n 3
  66. .. .
  67. Assuming a replacement policy of FIFO, and a cache of size 3 pages,
  68. figure out whether each of the following page references hit or miss
  69. in the page cache.
  70. Access: 5 Hit/Miss? State of Memory?
  71. Access: 4 Hit/Miss? State of Memory?
  72. Access: 5 Hit/Miss? State of Memory?
  73. ]
  74. As you can see, in this example, we specify "-n 3" which means the program
  75. should generate 3 random page references, which it does: 5, 7, and 5. The
  76. random seed is also specified (10), which is what gets us those particular
  77. numbers. After working this out yourself, have the program solve the problem
  78. for you by passing in the same arguments but with "-c" (showing just the
  79. relevant part here):
  80. prompt> ./paging-policy.py -s 10 -n 3 -c
  81. ...
  82. Solving...
  83. Access: 5 MISS FirstIn-> [br 5] <-Lastin Replace:- [br Hits:0 Misses:1]
  84. Access: 4 MISS FirstIn->[br 5, 4] <-Lastin Replace:- [br Hits:0 Misses:2]
  85. Access: 5 HIT FirstIn->[br 5, 4] <-Lastin Replace:- [br Hits:1 Misses:2]
  86. ]
  87. The default policy is FIFO, though others are available, including LRU, MRU,
  88. OPT (the optimal replacement policy, which peeks into the future to see what
  89. is best to replace), UNOPT (which is the pessimal replacement), RAND (which
  90. does random replacement), and CLOCK (which does the clock algorithm). The
  91. CLOCK algorithm also takes another argument (-b), which states how many bits
  92. should be kept per page; the more clock bits there are, the better the
  93. algorithm should be at determining which pages to keep in memory.
  94. Other options include: "-C/--cachesize" which changes the size of the page
  95. cache; "-m/--maxpage" which is the largest page number that will be used if
  96. the simulator is generating references for you; and "-f/--addressfile" which
  97. lets you specify a file with addresses in them, in case you wish to get traces
  98. from a real application or otherwise use a long trace as input.