HPCToolkit
VersatileMemoryPage.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/VersatileMemoryPage.cpp $
6 // $Id: VersatileMemoryPage.cpp 4380 2013-10-16 06:34:29Z felipet1326@gmail.com $
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/VersatileMemoryPage.cpp $
51 //
52 // Purpose:
53 // Manages pages of memory that are mapped and unmapped dynamically from the
54 // trace file based on the last time they were used.
55 //
56 // Description:
57 // [The set of functions, macros, etc. defined in the file]
58 //
59 //***************************************************************************
60 
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <cstring>
64 #include <list>
65 #include <errno.h>
66 
67 #include "DebugUtils.hpp"
68 #include "VersatileMemoryPage.hpp"
69 #include "LRUList.hpp"
70 
71 namespace TraceviewerServer
72 {
74 
76  {
77  startPoint = _startPoint;
78  size = _size;
79  mostRecentlyUsed = pageManagementList;
80  index = mostRecentlyUsed->addNewUnused(this);
81  file = _file;
82  isMapped = false;
83  if (MAX_PAGES_TO_ALLOCATE_AT_ONCE <1)
84  cerr<<"Set max pages before creating any VersatileMemoryPages"<<endl;
85  }
86 
88  {
89  MAX_PAGES_TO_ALLOCATE_AT_ONCE = pages;
90  }
91 
93  {
94  if (isMapped)
95  unmapPage();
96  }
98  {
99 
100  if (!isMapped)
101  {
102  mapPage();
103  }
104  mostRecentlyUsed->putOnTop(index);
105 
106  return page;
107  }
108 
110  {
111 
112  DEBUGCOUT(1) << "Mapping page "<< index<< " "<<mostRecentlyUsed->getUsedPageCount() << " / " << MAX_PAGES_TO_ALLOCATE_AT_ONCE << endl;
113 
114  if (isMapped)
115  {
116  cerr << "Trying to double map!"<<endl;
117  return;
118  }
119  if (mostRecentlyUsed->getUsedPageCount() >= MAX_PAGES_TO_ALLOCATE_AT_ONCE)
120  {
121 
122  VersatileMemoryPage* toRemove = mostRecentlyUsed->getLast();
123 
124  DEBUGCOUT(1)<<"Kicking " << toRemove->index << " out"<<endl;
125 
126  if (toRemove->isMapped != true)
127  cerr << "Least recently used one isn't even mapped?"<<endl;
128 
129  toRemove->unmapPage();
130  mostRecentlyUsed->removeLast();
131  }
132  page = (char*)mmap(0, size, MAP_PROT, MAP_FLAGS, file, startPoint);
133  if (page == MAP_FAILED)
134  {
135  cerr << "Mapping returned error " << strerror(errno) << endl;
136  cerr << "off_t size =" << sizeof(off_t) << "mapping size=" << size << " MapProt=" <<MAP_PROT
137  << " MapFlags=" << MAP_FLAGS << " fd=" << file << " Start point=" << startPoint << endl;
138  fflush(NULL);
139  exit(-1);
140  }
141 
142 
143  isMapped = true;
144  mostRecentlyUsed->reAdd(index);
145  }
147  {
148  if (!isMapped)
149  {
150  cerr << "Trying to double unmap!"<<endl;
151  return;
152  }
153  munmap(page, size);
154 
155  isMapped = false;
156 
157  DEBUGCOUT(1) << "Unmapped a page"<<endl;
158 
159  }
160 
161 } /* namespace TraceviewerServer */
exit
Definition: names.cpp:1
LRUList< VersatileMemoryPage > * mostRecentlyUsed
uint64_t FileOffset
Definition: FileUtils.hpp:78
#define DEBUGCOUT(a)
Definition: DebugUtils.hpp:72
static int MAX_PAGES_TO_ALLOCATE_AT_ONCE
#define NULL
Definition: ElfHelper.cpp:85