-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
168 lines (121 loc) · 4.21 KB
/
main.cc
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
// ======================================
// = Timothy Luciani and Rebecca Hachey =
// main.cc
// ======================================
#include <cstdio>
#include <cstdlib>
#include <time.h>
// String/File IO
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
// Module Class Definitions
#include "myPTM.hh"
#include "myScheduler.hh"
#include "myDM.hh"
// module initialization (May need to be an array of instances for each script)
myPTM *ptm = NULL;
myDM *dm = NULL;
// log of the actions in the main module
vector<string> mainLog;
std::string getTime(){
// get and return time in milliseconds
stringstream ss;
ss << "SysTime (ms):" << " " << time(0) * 1000;
return ss.str();
}
// ====================
// = Main Entry Point =
// Takes input from command line
// Parses and launches the appr.
// Modules
// ====================
vector<string> extract(std::string scr){
mainLog.push_back(getTime() + ": Read complete script file into memory.");
std::string file; // variable to read file into
std::ifstream script(scr.c_str()); // current files
std::vector<string> commands;
if (script.is_open()){ // if able to open file
/* read entire file */
script.seekg(0, std::ios::end);
file.reserve(script.tellg());
script.seekg(0, std::ios::beg);
file.assign((std::istreambuf_iterator<char>(script)),
std::istreambuf_iterator<char>());
script.close(); // close file
std::istringstream lines(file.c_str()); // iterate over the file lines
std::string line;
mainLog.push_back(getTime() + ": Break script up into lines: main memory again.");
while ( !lines.eof() ){ // continue until EOF
// get current line
getline(lines,line);
commands.push_back(line);
mainLog.push_back(getTime() + ": Line: " + line);
}
} else{ // FAIL!
mainLog.push_back(getTime() + ": Failed to open script: " + scr);
std::cout << "Unable to open file \n";
} // end else
return commands;
}
int main(int argc, char*argv[]){
/*
1. the maximum number of records/tuples that could be held in a data file (such as 2000);
2. the number of buffer pages;
3. the search method (scan or hash);
4. the method of concurrent reading from program files (round robin or random).
*/
mainLog.push_back(getTime() + ": Read in command line arguments");
stringstream ss;
int maxRecords = atoi(argv[1]);
ss << maxRecords;
mainLog.push_back(getTime() + ": Records: " + ss.str());
ss.clear();
int numBufferPages = atoi(argv[2]);
ss << numBufferPages;
mainLog.push_back(getTime() + ": Number of Buffer Pages: " + ss.str());
ss.clear();
int searchMode = atoi(argv[3]);
ss << searchMode;
mainLog.push_back(getTime() + ": Search Mode: " + ss.str());
ss.clear();
int readMode = atoi(argv[4]);
ss << readMode;
mainLog.push_back(getTime() + ": Read Mode: " + ss.str());
ss.clear();
string data_file = std::string(argv[5]);
// initialize the data manager
// dm = new myDM(searchMode, maxRecords, numBufferPages, data_file );
// mainLog.push_back(getTime() + ": Initialized Data Manager");
// number of threads to launch
const int NUM_THREADS = argc-6;
ss << NUM_THREADS;
mainLog.push_back(getTime() + ": Number of Scripts: " + ss.str());
ss.clear();
mainLog.push_back(getTime() + ": Seed random number generator");
/* initialize random seed: */
srand (time(NULL));
std::string names[NUM_THREADS];
/* launch threads to work with the scripts*/
std::vector< std::vector<string> > transactions;
mainLog.push_back(getTime() + ": Parse script files.");
for(int i = 0; i < NUM_THREADS; i++){
// get the name of the script
names[i] = std::string(argv[i+6]);
mainLog.push_back(getTime() + ": Script: " + names[i]);
transactions.push_back( extract( names[i] ) );
}
mainLog.push_back(getTime() + ": Launch Transaction Manager");
string mode = (readMode == 0) ? "Round Robin" : "Random";
mainLog.push_back(getTime() + ": Mode: " + mode);
// launch transaction manager
ptm = new myPTM(transactions, readMode, searchMode, maxRecords, numBufferPages, data_file);
/* writing transaction log out */
ofstream log;
log.open ("mainLog.txt");
for(int i = 0; i < mainLog.size(); i++)
log << mainLog[i] << "\n";
log.close();
return 0;
} // end main