forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageFileManager_ReadAhead.cpp
204 lines (176 loc) · 5.39 KB
/
RageFileManager_ReadAhead.cpp
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
#include "global.h"
#include "RageFileManager_ReadAhead.h"
#include "RageThreads.h"
#include "RageLog.h"
#if defined(HAVE_FCNTL_H)
#include <fcntl.h>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#endif
#include <cerrno>
#if defined(WIN32)
#include <io.h>
#endif
#if defined(HAVE_POSIX_FADVISE)
void RageFileManagerReadAhead::Init() { }
void RageFileManagerReadAhead::Shutdown() { }
void RageFileManagerReadAhead::ReadAhead( RageFileBasic *pFile, int iBytes )
{
int iFD = pFile->GetFD();
if( iFD == -1 )
return;
int iStart = lseek( iFD, 0, SEEK_CUR );
posix_fadvise( iFD, iStart, iBytes, POSIX_FADV_WILLNEED );
}
void RageFileManagerReadAhead::DiscardCache( RageFileBasic *pFile, int iRelativePosition, int iBytes )
{
int iFD = pFile->GetFD();
if( iFD == -1 )
return;
int iStart = lseek( iFD, 0, SEEK_CUR );
iStart += iRelativePosition;
if( iStart < 0 )
{
iBytes -= iStart;
iStart = 0;
}
if( iBytes == 0 )
return;
posix_fadvise( iFD, iStart, iBytes, POSIX_FADV_DONTNEED );
}
#else
#if 0
/* This doesn't currently work, because dup() locks the file position of the new FD with the old
* (which makes no sense at all), so it's not easy to read from a copy of the FD without
* interfering with the original. */
class RageFileReadAheadThread
{
public:
RageFileReadAheadThread( int iFD, int iFrom, int iBytes )
{
m_iFD = iFD;
m_iFrom = iFrom;
m_iBytes = iBytes;
m_bFinished = false;
m_WorkerThread.SetName( "Read-ahead thread" );
m_WorkerThread.Create( StartWorkerMain, this );
}
~RageFileReadAheadThread()
{
close( m_iFD );
m_WorkerThread.Wait();
}
bool IsFinished() const { return m_bFinished; }
private:
static int StartWorkerMain( void *pThis )
{
((RageFileReadAheadThread *) (pThis))->WorkerMain();
return 0;
}
void WorkerMain()
{
int iFDCopy = dup( m_iFD );
if( iFDCopy != -1 )
{
char [] buf = new char[m_iBytes];
lseek( iFDCopy, m_iFrom, SEEK_SET );
read( iFDCopy, buf, m_iBytes );
delete [] buf;
close( iFDCopy );
LOG->Trace("read");
}
m_bFinished = true;
}
RageThread m_WorkerThread;
bool m_bFinished;
int m_iFD;
int m_iFrom;
int m_iBytes;
};
static vector<RageFileReadAheadThread *> g_apReadAheads;
void RageFileManagerReadAhead::Init() { }
void RageFileManagerReadAhead::Shutdown()
{
for (auto *read: g_apReadAheads)
{
delete read;
}
g_apReadAheads.clear();
}
/* This emulates posix_fadvise(POSIX_FADV_WILLNEED), to force data into cache and hint the
* kernel to start read-ahead from that point. */
void RageFileManagerReadAhead::ReadAhead( RageFileBasic *pFile, int iBytes )
{
int iFD = pFile->GetFD();
if( iFD == -1 )
return;
iFD = dup( iFD );
if( iFD == -1 )
{
LOG->Trace( "error: dup(): %s", strerror(errno) );
return;
}
int iStart = lseek( iFD, 0, SEEK_CUR );
RageFileReadAheadThread *pReadAhead = new RageFileReadAheadThread( iFD, iStart, iBytes );
g_apReadAheads.push_back( pReadAhead );
for( size_t i = 0; i < g_apReadAheads.size(); ++i )
{
if( g_apReadAheads[i]->IsFinished() )
{
LOG->Trace("cleaned");
delete g_apReadAheads[i];
g_apReadAheads.erase( g_apReadAheads.begin()+i, g_apReadAheads.begin()+i+1 );
--i;
}
}
}
void RageFileManagerReadAhead::DiscardCache( RageFileBasic *pFile, int iRelativePosition, int iBytes ) { }
#else
void RageFileManagerReadAhead::Init() { }
void RageFileManagerReadAhead::Shutdown() { }
void RageFileManagerReadAhead::ReadAhead( RageFileBasic *pFile, int iBytes ) { }
void RageFileManagerReadAhead::DiscardCache( RageFileBasic *pFile, int iRelativePosition, int iBytes ) { }
#endif
#endif
void RageFileManagerReadAhead::CacheHintStreaming( RageFileBasic *pFile )
{
#if defined(HAVE_POSIX_FADVISE)
/* This guesses at the actual size of the file on disk, which may be smaller if this file is compressed.
* Since this is usually used on music and video files, it generally shouldn't be. */
int iFD = pFile->GetFD();
if( iFD == -1 )
return;
int iPos = pFile->Tell();
int iFrom = lseek( iFD, 0, SEEK_CUR );
int iBytes = pFile->GetFileSize() - iPos;
posix_fadvise( iFD, iFrom, iBytes, POSIX_FADV_SEQUENTIAL );
#endif
}
/*
* Copyright (c) 2010 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/