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
{
73
static
int
MAX_PAGES_TO_ALLOCATE_AT_ONCE
= 0;
74
75
VersatileMemoryPage::VersatileMemoryPage
(
FileOffset
_startPoint,
int
_size,
FileDescriptor
_file,
LRUList<VersatileMemoryPage>
* pageManagementList)
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
87
void
VersatileMemoryPage::setMaxPages
(
int
pages)
88
{
89
MAX_PAGES_TO_ALLOCATE_AT_ONCE = pages;
90
}
91
92
VersatileMemoryPage::~VersatileMemoryPage
()
93
{
94
if
(
isMapped
)
95
unmapPage
();
96
}
97
char
*
VersatileMemoryPage::get
()
98
{
99
100
if
(!
isMapped
)
101
{
102
mapPage
();
103
}
104
mostRecentlyUsed
->putOnTop(
index
);
105
106
return
page
;
107
}
108
109
void
VersatileMemoryPage::mapPage
()
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
}
146
void
VersatileMemoryPage::unmapPage
()
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 */
TraceviewerServer::VersatileMemoryPage::get
char * get()
Definition:
VersatileMemoryPage.cpp:97
TraceviewerServer::VersatileMemoryPage::VersatileMemoryPage
VersatileMemoryPage()
TraceviewerServer::VersatileMemoryPage::index
int index
Definition:
VersatileMemoryPage.hpp:87
TraceviewerServer::VersatileMemoryPage
Definition:
VersatileMemoryPage.hpp:72
TraceviewerServer
Definition:
BaseDataFile.cpp:66
TraceviewerServer::VersatileMemoryPage::mapPage
void mapPage()
Definition:
VersatileMemoryPage.cpp:109
TraceviewerServer::LRUList
Definition:
LRUList.hpp:75
TraceviewerServer::VersatileMemoryPage::isMapped
bool isMapped
Definition:
VersatileMemoryPage.hpp:90
TraceviewerServer::VersatileMemoryPage::unmapPage
void unmapPage()
Definition:
VersatileMemoryPage.cpp:146
TraceviewerServer::VersatileMemoryPage::file
FileDescriptor file
Definition:
VersatileMemoryPage.hpp:88
exit
exit
Definition:
names.cpp:1
TraceviewerServer::FileDescriptor
int FileDescriptor
Definition:
FileUtils.hpp:77
TraceviewerServer::VersatileMemoryPage::mostRecentlyUsed
LRUList< VersatileMemoryPage > * mostRecentlyUsed
Definition:
VersatileMemoryPage.hpp:91
TraceviewerServer::FileOffset
uint64_t FileOffset
Definition:
FileUtils.hpp:78
LRUList.hpp
TraceviewerServer::VersatileMemoryPage::setMaxPages
static void setMaxPages(int)
Definition:
VersatileMemoryPage.cpp:87
TraceviewerServer::VersatileMemoryPage::size
int size
Definition:
VersatileMemoryPage.hpp:85
TraceviewerServer::VersatileMemoryPage::MAP_FLAGS
static const int MAP_FLAGS
Definition:
VersatileMemoryPage.hpp:97
DEBUGCOUT
#define DEBUGCOUT(a)
Definition:
DebugUtils.hpp:72
TraceviewerServer::MAX_PAGES_TO_ALLOCATE_AT_ONCE
static int MAX_PAGES_TO_ALLOCATE_AT_ONCE
Definition:
VersatileMemoryPage.cpp:73
NULL
#define NULL
Definition:
ElfHelper.cpp:85
TraceviewerServer::VersatileMemoryPage::page
char * page
Definition:
VersatileMemoryPage.hpp:86
VersatileMemoryPage.hpp
TraceviewerServer::VersatileMemoryPage::~VersatileMemoryPage
virtual ~VersatileMemoryPage()
Definition:
VersatileMemoryPage.cpp:92
TraceviewerServer::VersatileMemoryPage::startPoint
FileOffset startPoint
Definition:
VersatileMemoryPage.hpp:84
DebugUtils.hpp
src
tool
hpcserver
VersatileMemoryPage.cpp
Generated by
1.8.13