gem5  v20.0.0.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
flash_device.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
53 #include "dev/arm/flash_device.hh"
54 
55 #include "base/trace.hh"
56 #include "debug/Drain.hh"
57 
63 FlashDeviceParams::create()
64 {
65  return new FlashDevice(this);
66 }
67 
68 
73 FlashDevice::FlashDevice(const FlashDeviceParams* p):
74  AbstractNVM(p),
75  diskSize(0),
76  blockSize(p->blk_size),
77  pageSize(p->page_size),
78  GCActivePercentage(p->GC_active),
79  readLatency(p->read_lat),
80  writeLatency(p->write_lat),
81  eraseLatency(p->erase_lat),
82  dataDistribution(p->data_distribution),
83  numPlanes(p->num_planes),
84  pagesPerBlock(0),
85  pagesPerDisk(0),
86  blocksPerDisk(0),
87  planeMask(numPlanes - 1),
88  planeEventQueue(numPlanes),
89  planeEvent([this]{ actionComplete(); }, name())
90 {
91 
92  /*
93  * Let 'a' be a power of two of n bits, written such that a-n is the msb
94  * and a-0 is the lsb. Since it is a power of two, only one bit (a-x,
95  * with 0 <= x <= n) is set. If we subtract one from this number the bits
96  * a-(x-1) to a-0 are set and all the other bits are cleared. Hence a
97  * bitwise AND with those two numbers results in an integer with all bits
98  * cleared.
99  */
100  if (numPlanes & planeMask)
101  fatal("Number of planes is not a power of 2 in flash device.\n");
102 }
103 
109 void
110 FlashDevice::initializeFlash(uint64_t disk_size, uint32_t sector_size)
111 {
112  diskSize = disk_size * sector_size;
116 
118  DPRINTF(FlashDevice, "diskSize: %d Bytes; %d pages per block, %d pages "
119  "per disk\n", diskSize, pagesPerBlock, pagesPerDisk);
120 
121  locationTable.resize(pagesPerDisk);
122 
124  blockValidEntries.resize(blocksPerDisk, 0);
126 
136  unknownPages.resize((pagesPerDisk >> 5) + 1, 0xFFFFFFFF);
137 
138  for (uint32_t count = 0; count < pagesPerDisk; count++) {
139  //setup lookup table + physical aspects
140 
141  if (dataDistribution == Enums::stripe) {
144 
145  } else {
148  }
149  }
150 }
151 
153 {
154  DPRINTF(FlashDevice, "Remove FlashDevice\n");
155 }
156 
162 void
163 FlashDevice::accessDevice(uint64_t address, uint32_t amount, Callback *event,
164  Actions action)
165 {
166  DPRINTF(FlashDevice, "Flash calculation for %d bytes in %d pages\n"
167  , amount, pageSize);
168 
169  std::vector<Tick> time(numPlanes, 0);
170  uint64_t logic_page_addr = address / pageSize;
171  uint32_t plane_address = 0;
172 
180  for (uint32_t count = 0; amount > (count * pageSize); count++) {
181  uint32_t index = (locationTable[logic_page_addr].block *
182  pagesPerBlock) + (logic_page_addr % pagesPerBlock);
183 
184  DPRINTF(FlashDevice, "Index 0x%8x, Block 0x%8x, pages/block %d,"
185  " logic address 0x%8x\n", index,
186  locationTable[logic_page_addr].block, pagesPerBlock,
187  logic_page_addr);
188  DPRINTF(FlashDevice, "Page %d; %d bytes up to this point\n", count,
189  (count * pageSize));
190 
191  plane_address = locationTable[logic_page_addr].block & planeMask;
192 
193  if (action == ActionRead) {
194  //lookup
195  //call accessTimes
196  time[plane_address] += accessTimes(locationTable[logic_page_addr]
197  .block, ActionRead);
198 
199  /*stats*/
200  stats.readAccess.sample(logic_page_addr);
201  stats.readLatency.sample(time[plane_address]);
202  } else { //write
203  //lookup
204  //call accessTimes if appropriate, page may be unknown, so lets
205  //give it the benefit of the doubt
206 
207  if (getUnknownPages(index))
208  time[plane_address] += accessTimes
209  (locationTable[logic_page_addr].block, ActionWrite);
210 
211  else //A remap is needed
212  time[plane_address] += remap(logic_page_addr);
213 
214  /*stats*/
215  stats.writeAccess.sample(logic_page_addr);
216  stats.writeLatency.sample(time[plane_address]);
217  }
218 
226  if (getUnknownPages(index)) {
227  clearUnknownPages(index);
228  --blockEmptyEntries[locationTable[logic_page_addr].block];
229  ++blockValidEntries[locationTable[logic_page_addr].block];
230  }
231 
232  stats.fileSystemAccess.sample(address);
233  ++logic_page_addr;
234  }
235 
240  for (uint32_t count = 0; count < numPlanes; count++){
241  plane_address = (time[plane_address] > time[count]) ? plane_address
242  : count;
243 
244  DPRINTF(FlashDevice, "Plane %d is busy for %d ticks\n", count,
245  time[count]);
246 
247  if (time[count] != 0) {
248 
249  struct CallBackEntry cbe;
256  if (planeEventQueue[count].empty())
257  cbe.time = time[count] + curTick();
258  else
259  cbe.time = time[count] +
260  planeEventQueue[count].back().time;
261  cbe.function = NULL;
262  planeEventQueue[count].push_back(cbe);
263 
264  DPRINTF(FlashDevice, "scheduled at: %ld\n", cbe.time);
265 
266  if (!planeEvent.scheduled())
267  schedule(planeEvent, planeEventQueue[count].back().time);
268  else if (planeEventQueue[count].back().time < planeEvent.when())
270  planeEventQueue[plane_address].back().time, true);
271  }
272  }
273 
274  //worst case two plane finish at the same time, each triggers an event
275  //and this callback will be called once. Maybe before the other plane
276  //could execute its event, but in the same tick.
277  planeEventQueue[plane_address].back().function = event;
278  DPRINTF(FlashDevice, "Callback queued for plane %d; %d in queue\n",
279  plane_address, planeEventQueue[plane_address].size());
280  DPRINTF(FlashDevice, "first event @ %d\n", planeEvent.when());
281 }
282 
288 void
290 {
291  DPRINTF(FlashDevice, "Plane action completed\n");
292  uint8_t plane_address = 0;
293 
294  uint8_t next_event = 0;
295 
297  for (plane_address = 0; plane_address < numPlanes; plane_address++) {
298  if (!planeEventQueue[plane_address].empty()) {
303  assert(planeEventQueue[plane_address].front().time >= curTick());
304 
305  if (planeEventQueue[plane_address].front().time == curTick()) {
311  Callback *temp = planeEventQueue[plane_address].front().
312  function;
313  planeEventQueue[plane_address].pop_front();
314 
316  if (temp != NULL) {
317  DPRINTF(FlashDevice, "Callback, %d\n", plane_address);
318  temp->process();
319  }
320  }
321  }
322  }
323 
325  for (plane_address = 0; plane_address < numPlanes; plane_address++) {
326  if (!planeEventQueue[plane_address].empty())
327  if (planeEventQueue[next_event].empty() ||
328  (planeEventQueue[plane_address].front().time <
329  planeEventQueue[next_event].front().time))
330  next_event = plane_address;
331  }
332 
334  if (!planeEventQueue[next_event].empty()) {
335  DPRINTF(FlashDevice, "Schedule plane: %d\n", plane_address);
336  reschedule(planeEvent, planeEventQueue[next_event].front().time, true);
337  }
338 
339  checkDrain();
340 
341  DPRINTF(FlashDevice, "returing from flash event\n");
342  DPRINTF(FlashDevice, "first event @ %d\n", planeEvent.when());
343 }
344 
350 Tick
351 FlashDevice::remap(uint64_t logic_page_addr)
352 {
356  if (blockEmptyEntries[locationTable[logic_page_addr].block] > 0) {
357  //just a remap
358  //update tables
359  --blockEmptyEntries[locationTable[logic_page_addr].block];
360  //access to this table won't be sequential anymore
361  locationTable[logic_page_addr].page = pagesPerBlock + 2;
362  //access new block
363  Tick time = accessTimes(locationTable[logic_page_addr].block,
364  ActionWrite);
365 
366  DPRINTF(FlashDevice, "Remap returns %d ticks\n", time);
367  return time;
368 
369  } else {
370  //calculate how much time GC would have taken
371  uint32_t block = locationTable[logic_page_addr].block;
372  Tick time = ((GCActivePercentage *
373  (accessTimes(block, ActionCopy) +
374  accessTimes(block, ActionErase)))
375  / 100);
376 
377  //use block as the logical start address of the block
378  block = locationTable[logic_page_addr].block * pagesPerBlock;
379 
380  //assumption: clean will improve locality
381  for (uint32_t count = 0; count < pagesPerBlock; count++) {
382  assert(block + count < pagesPerDisk);
383  locationTable[block + count].page = (block + count) %
384  pagesPerBlock;
385  }
386 
387  blockEmptyEntries[locationTable[logic_page_addr].block] =
389  /*stats*/
391 
392  DPRINTF(FlashDevice, "Remap with erase action returns %d ticks\n",
393  time);
394 
395  return time;
396  }
397 
398 }
399 
403 Tick
404 FlashDevice::accessTimes(uint64_t block, Actions action)
405 {
406  Tick time = 0;
407 
408  switch(action) {
409  case ActionRead: {
411  time = readLatency;
412  } break;
413 
414  case ActionWrite: {
416  time = writeLatency + readLatency;
417  } break;
418 
419  case ActionErase: {
421  time = eraseLatency + readLatency;
422  } break;
423 
424  case ActionCopy: {
426  uint32_t validpages = blockValidEntries[block];
427  time = validpages * (readLatency + writeLatency);
428  } break;
429 
430  default: break;
431  }
432 
433  //Used to determine sequential action.
434  DPRINTF(FlashDevice, "Access returns %d ticks\n", time);
435  return time;
436 }
437 
452 inline
453 void
455 {
456  unknownPages[index >> 5] &= ~(0x01 << (index % 32));
457 }
458 
463 inline
464 bool
466 {
467  return unknownPages[index >> 5] & (0x01 << (index % 32));
468 }
469 
470 void
472 {
474 
475  using namespace Stats;
476 
477  std::string fd_name = name() + ".FlashDevice";
478 
479  // Register the stats
482  .name(fd_name + ".totalGCActivations")
483  .desc("Number of Garbage collector activations")
484  .flags(none);
485 
488  .init(2)
489  .name(fd_name + ".writeAccessHist")
490  .desc("Histogram of write addresses")
491  .flags(pdf);
493  .init(2)
494  .name(fd_name + ".readAccessHist")
495  .desc("Histogram of read addresses")
496  .flags(pdf);
498  .init(100)
499  .name(fd_name + ".fileSystemAccessHist")
500  .desc("Histogram of file system accesses")
501  .flags(pdf);
502 
505  .init(100)
506  .name(fd_name + ".writeLatencyHist")
507  .desc("Histogram of write latency")
508  .flags(pdf);
510  .init(100)
511  .name(fd_name + ".readLatencyHist")
512  .desc("Histogram of read latency")
513  .flags(pdf);
514 }
515 
520 void
522 {
524 
528 
529  int location_table_size = locationTable.size();
530  SERIALIZE_SCALAR(location_table_size);
531  for (uint32_t count = 0; count < location_table_size; count++) {
532  paramOut(cp, csprintf("locationTable[%d].page", count),
533  locationTable[count].page);
534  paramOut(cp, csprintf("locationTable[%d].block", count),
535  locationTable[count].block);
536  }
537 };
538 
543 void
545 {
547 
551 
552  int location_table_size;
553  UNSERIALIZE_SCALAR(location_table_size);
554  locationTable.resize(location_table_size);
555  for (uint32_t count = 0; count < location_table_size; count++) {
556  paramIn(cp, csprintf("locationTable[%d].page", count),
557  locationTable[count].page);
558  paramIn(cp, csprintf("locationTable[%d].block", count),
559  locationTable[count].block);
560  }
561 };
562 
569 {
570  if (planeEvent.scheduled()) {
571  DPRINTF(Drain, "Flash device is draining...\n");
572  return DrainState::Draining;
573  } else {
574  DPRINTF(Drain, "Flash device in drained state\n");
575  return DrainState::Drained;
576  }
577 }
578 
583 void
585 {
587  return;
588 
589  if (planeEvent.when() > curTick()) {
590  DPRINTF(Drain, "Flash device is still draining\n");
591  } else {
592  DPRINTF(Drain, "Flash device is done draining\n");
593  signalDrainDone();
594  }
595 }
count
Definition: misc.hh:703
uint32_t planeMask
uint32_t pagesPerBlock
Disk dimensions in pages and blocks.
#define DPRINTF(x,...)
Definition: trace.hh:222
void serialize(CheckpointOut &cp) const override
Serialize; needed to create checkpoints.
const FlagsType pdf
Print the percent of the total that this entry represents.
Definition: info.hh:51
void actionComplete()
Event rescheduler.
const uint32_t blockSize
Bitfield< 30, 0 > index
virtual void process()=0
virtual process function that is invoked when the callback queue is executed.
void regStats() override
Stats register function.
std::vector< uint32_t > blockEmptyEntries
number of empty entries
#define UNSERIALIZE_CONTAINER(member)
Definition: serialize.hh:829
const uint32_t numPlanes
Stats::Scalar totalGCActivations
Amount of GC activations.
Definition: flash_device.hh:95
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:171
Generic callback class.
Definition: callback.hh:39
const Tick eraseLatency
Stats::Histogram writeAccess
Histogram of address accesses.
Definition: flash_device.hh:98
std::vector< struct PageMapEntry > locationTable
address to logic place has a block and a page field
std::vector< uint32_t > unknownPages
when the disk is first started we are unsure of the number of used pages, this variable will help det...
Stats::Histogram fileSystemAccess
Stats::Histogram writeLatency
Histogram of access latencies.
void unserialize(CheckpointIn &cp) override
Unserialize; needed to restore from checkpoints.
Tick accessTimes(uint64_t address, Actions accesstype)
Access time calculator.
Histogram & init(size_type size)
Set the parameters of this histogram.
Definition: statistics.hh:2641
Definition: cprintf.cc:40
const Enums::DataDistribution dataDistribution
Flash organization.
void clearUnknownPages(uint32_t index)
Function to indicate that a page is known.
Derived & flags(Flags _flags)
Set the flags and marks this stat to print at the end of simulation.
Definition: statistics.hh:333
DrainState
Object drain/handover states.
Definition: drain.hh:71
DrainState drain() override
Checkpoint functions.
DrainState drainState() const
Return the current drain state of an object.
Definition: drain.hh:308
void checkDrain()
Checkdrain; needed to enable checkpoints.
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:770
Draining buffers pending serialization/handover.
void accessDevice(uint64_t address, uint32_t amount, Callback *event, Actions action)
Flash action function.
Tick curTick()
The current simulated tick.
Definition: core.hh:44
FlashDevice(const FlashDeviceParams *)
Initialize functions.
Definition: flash_device.cc:73
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
struct FlashDeviceStats stats
RequestHandler stats.
uint64_t Tick
Tick count type.
Definition: types.hh:61
A copy involves taking all the used pages from a block and store it in another.
Definition: flash_device.hh:79
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:38
const Tick readLatency
Access latencies.
void schedule(Event &event, Tick when)
Definition: eventq.hh:998
const FlagsType none
Nothing extra to print.
Definition: info.hh:43
std::vector< uint32_t > blockValidEntries
number of valid entries per block
void reschedule(Event &event, Tick when, bool always=false)
Definition: eventq.hh:1016
Bitfield< 10, 5 > event
Flash Device model The Flash Device model is a timing model for a NAND flash device.
Definition: flash_device.hh:54
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:763
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:460
std::vector< std::deque< struct CallBackEntry > > planeEventQueue
This vector of queues keeps track of all the callbacks per plane.
Derived & name(const std::string &name)
Set the name and marks this stat to print at the end of simulation.
Definition: statistics.hh:276
virtual const std::string name() const
Definition: sim_object.hh:128
#define SERIALIZE_CONTAINER(member)
Definition: serialize.hh:821
std::ostream CheckpointOut
Definition: serialize.hh:63
Actions
Defines the possible actions to the flash.
Definition: flash_device.hh:71
int amount
Definition: qarma.hh:67
void signalDrainDone() const
Signal that an object is drained.
Definition: drain.hh:289
EventFunctionWrapper planeEvent
Completion event.
Tick remap(uint64_t logic_page_addr)
FTL functionality.
const uint32_t pageSize
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:69
This is an interface between the disk interface (which will handle the disk data transactions) and th...
Definition: abstract_nvm.hh:53
uint32_t blocksPerDisk
Derived & desc(const std::string &_desc)
Set the description and marks this stat to print at the end of simulation.
Definition: statistics.hh:309
bool getUnknownPages(uint32_t index)
Function to test if a page is known.
uint64_t diskSize
Disk sizes in bytes.
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
const uint32_t GCActivePercentage
Garbage collection algorithm emulator.
Bitfield< 0 > p
Running normally.
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:500
uint32_t pagesPerDisk
void sample(const U &v, int n=1)
Add a value to the distribtion n times.
Definition: statistics.hh:1896
const Tick writeLatency
void initializeFlash(uint64_t disk_size, uint32_t sector_size)
Initialization function; called when all disk specifics are known.

Generated on Mon Jun 8 2020 15:45:09 for gem5 by doxygen 1.8.13