-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcIORecorder.hxx
74 lines (55 loc) · 1.73 KB
/
ProcIORecorder.hxx
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
#ifndef __ProcIORecorder_hxx__
#define __ProcIORecorder_hxx__
#include <sys/types.h>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include "FilenameInternTable.hxx"
class ProcIORecorder
{
typedef FilenameInternTable::id_type file_id_type;
public:
void record_process_input(const pid_t& pid, const char *fname)
{
get_node(pid).infile_ids.insert(ftable_.id(fname));
}
void record_process_output(const pid_t& pid, const char *fname)
{
get_node(pid).outfile_ids.insert(ftable_.id(fname));
}
const FilenameInternTable& get_filename_table() const
{
return ftable_;
}
typedef std::unordered_multimap<file_id_type, file_id_type> dependency_map_type;
void populate_dependency_map(dependency_map_type &depmap) const
{
for (auto const &r : records_)
for (auto const &ofid : r.second->outfile_ids)
for (auto const &ifid : r.second->infile_ids)
depmap.insert(std::make_pair(ofid, ifid));
}
private:
struct ProcIORecord
{
const pid_t pid;
std::unordered_set<unsigned> infile_ids;
std::unordered_set<unsigned> outfile_ids;
ProcIORecord(const pid_t& procid)
: pid(procid)
{}
};
std::unordered_map<pid_t, std::unique_ptr<ProcIORecord>> records_;
FilenameInternTable ftable_;
ProcIORecord& get_node(const pid_t& pid)
{
if (records_.count(pid) == 0) /* new process id */ {
std::unique_ptr<ProcIORecord> pnode( new ProcIORecord(pid) );
records_.insert(std::make_pair(pid, std::move(pnode)));
}
return *records_[pid];
}
};
#endif // include guard