This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathresset.cc
executable file
·134 lines (118 loc) · 3.47 KB
/
resset.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
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "util/resset.h"
#include "base/casts.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/strtoint.h"
#include "strings/split.h"
#include "strings/strip.h"
#include "re2/re2.h"
using ::std::vector;
namespace util {
// Useful functions/types for operating on sets of integers, as used
// for memory and cpu assignments and process listings.
// Read a sequence of ranges (separated by 'sep' from a file)
void ResSet::ReadSet(const string& path, const char *sep) {
char buf[4096];
int fd;
size_t bytes;
// Open the file and read the data into a list of ranges
CHECK((fd = open(path.c_str(), O_RDONLY)) != -1) << "Couldn't open " << path;
CHECK((bytes = read(fd, buf, sizeof(buf) - 1)) != -1);
buf[bytes] = 0;
close(fd);
ReadSetString(buf, sep);
}
void ResSet::ReadSetString(const string& buf, const char *sep) {
clear();
vector<string> list =
strings::Split(buf, strings::delimiter::AnyOf(sep), strings::SkipEmpty());
for (int i = 0; i < list.size(); i++) {
int first, last;
string field = list[i];
string laststr;
if (field == "\n") continue;
// Match against either a single integer, or a range
CHECK(RE2::FullMatch(field, "(\\d+)(?:-(\\d+))?\\n?", &first, &laststr))
<< " Invalid field '" << field << "', length " << field.size()
<< " char " << static_cast<int>(field[0]);
if (laststr.empty()) {
last = first;
} else {
last = atoi32(laststr.c_str());
}
// Fill in the range (or single value, if first==last)
while (first <= last) {
insert(first++);
}
}
}
bool ResSet::AppendTaskSet(const string& path) {
FILE *f = fopen(path.c_str(), "r");
if (!f)
return false;
int pid;
while (fscanf(f, "%d\n", &pid) == 1) {
insert(pid);
}
fclose(f);
return true;
}
void ResSet::ReadTaskSet(const string& path) {
clear();
CHECK(AppendTaskSet(path)) << "Couldn't open " << path;
}
void ResSet::ReadTaskSetQuiet(const string& path) {
clear();
AppendTaskSet(path);
}
ResSet& ResSet::operator= (const set<int>& ints) {
set<int>::operator= (ints);
return *this;
}
void ResSet::Format(string *str) const {
const char *sep = "";
int last = -2, first = -2;
for (ResSet::const_iterator it = begin(); it != end(); ++it) {
int node = *it;
if (node != last + 1) {
if (last >= 0) {
if (first == last) {
StringAppendF(str, "%s%d", sep, first);
} else {
StringAppendF(str, "%s%d-%d", sep, first, last);
}
sep = ",";
}
first = last = node;
} else {
last = node;
}
}
if (last >= 0) {
if (first == last) {
StringAppendF(str, "%s%d", sep, first);
} else {
StringAppendF(str, "%s%d-%d", sep, first, last);
}
}
StripTrailingWhitespace(str);
}
} // namespace util