-
Notifications
You must be signed in to change notification settings - Fork 3
/
bufferman.cpp
100 lines (83 loc) · 2.63 KB
/
bufferman.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
/*! \file
\brief holds main() function
*/
#define PATH ""
#define DEBUGMODE 0
#include "database.hpp"
#include "general.hpp"
#include "bufferman.hpp"
#include "tokenizer.hpp"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
if (argc > 2)
{
cerr << "Usage: " << argv[0] << " granularity" << endl;
return 0;
}
int granularity(0);
if (argc == 2) granularity = atoi(argv[1]);
char * filename = "stdin";
//ifstream in("E:/Documents/2002-02/2/Database/bufferman/out.txt");
istream & in = cin;
try
{
System system(PATH "system.dsc");
Database database(PATH "database.dsc", system);
BufferManager bm(system, database);
Tokenizer t(in);
while(t.next())
{
if (t.token != "read")
throw ParseException(filename, t, "Expecting 'read'", __FILE__, __LINE__);
int fileNum, startRecord, endRecord, priority, startSector, endSector;
bool eof = !t.next();
fileNum = t.getInt();
if (eof) throw ParseException(filename, t, "Expecting file number.", __FILE__, __LINE__);
eof = !t.next();
startRecord = t.getInt();
if (eof || startRecord <= 0)
throw ParseException(filename, t, "Expecting positive starting record number", __FILE__, __LINE__);
eof = !t.next();
endRecord = t.getInt();
if (eof || endRecord < startRecord)
throw ParseException(filename, t, "Expecting ending record after the starting record", __FILE__, __LINE__);
eof = !t.next();
priority = t.getInt();
if (eof || priority < -1 || priority > 99)
throw ParseException(filename, t, "Expecting priority in [-1,99]", __FILE__, __LINE__);
try
{
int reads = bm.read(fileNum, startRecord, endRecord, priority == -1 ? 10 : priority, startSector, endSector);
#if DEBUGMODE
cerr << "Reading " << setw(2) << fileNum << " " << setw(5)
<< startRecord << " " << setw(5) << endRecord << " blocks ["
<< startSector << "," << endSector << "] " << reads << " fetches" << endl;
#endif
if (granularity > 0 && bm.readNumber % granularity == 0)
cout << bm.time << endl;
// cout << bm.fetchNumber << endl;
}
catch(...)
{
cerr << "Error simulating read at " << filename << ":" << t.lineNo << endl;
throw;
}
};
cout << bm.time << endl;
cerr << "Total number of physical disk reads: " << bm.fetchNumber << endl;
return 0;
}
catch(Exception e)
{
cerr << e << endl;
}
catch(...)
{
cerr << "Unknown exception caught" << endl;
}
return 1;
}