Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Martin Reinecke
ducc
Commits
52df59d3
Commit
52df59d3
authored
Dec 27, 2019
by
Martin Reinecke
Browse files
more stuff
parent
c351cbf2
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/error_handling.cc
View file @
52df59d3
#include
"error_handling.h"
#include
"
mr_util/
error_handling.h"
using
namespace
std
;
namespace
mr
{
namespace
error_handling
{
namespace
detail
{
bool
abort_in_progress__
=
false
;
ostream
&
CodeLocation
::
print
(
ostream
&
os
)
const
...
...
@@ -16,3 +22,5 @@ void killjob__()
// perhaps print stack trace?
exit
(
1
);
}
}}}
src/mr_util/datatypes.h
View file @
52df59d3
...
...
@@ -31,37 +31,21 @@
* \author Martin Reinecke
*/
#ifndef MR
BASE
_DATATYPES_H
#define MR
BASE
_DATATYPES_H
#ifndef MR
UTIL
_DATATYPES_H
#define MR
UTIL
_DATATYPES_H
#include
<string>
#include
<cstddef>
#include
<cstdint>
#include
"error_handling.h"
#include
"
mr_util/
error_handling.h"
using
int8
=
std
::
int8_t
;
using
uint8
=
std
::
uint8_t
;
namespace
mr
{
using
int16
=
std
::
int16_t
;
using
uint16
=
std
::
uint16_t
;
using
int32
=
std
::
int32_t
;
using
uint32
=
std
::
uint32_t
;
using
int64
=
std
::
int64_t
;
using
uint64
=
std
::
uint64_t
;
using
float32
=
float
;
using
float64
=
double
;
/*! unsigned integer type which should be used for array sizes */
using
tsize
=
std
::
size_t
;
/*! signed integer type which should be used for relative array indices */
using
tdiff
=
std
::
ptrdiff_t
;
namespace
datatypes
{
/*! Returns a C string describing the data type \a T. */
template
<
typename
T
>
inline
const
char
*
type2typename
()
{
my
fail
(
T
::
UNSUPPORTED_DATA_TYPE
);
}
{
MR_
fail
(
T
::
UNSUPPORTED_DATA_TYPE
);
}
template
<
>
inline
const
char
*
type2typename
<
signed
char
>
()
{
return
"signed char"
;
}
template
<
>
inline
const
char
*
type2typename
<
unsigned
char
>
()
...
...
@@ -114,7 +98,7 @@ enum NDT {
/*! Returns the \a NDT constant associated with \a T. */
template
<
typename
T
>
inline
NDT
nativeType
()
{
my
fail
(
T
::
UNSUPPORTED_DATA_TYPE
);
}
{
MR_
fail
(
T
::
UNSUPPORTED_DATA_TYPE
);
}
template
<
>
inline
NDT
nativeType
<
char
>
()
{
return
NAT_CHAR
;
}
template
<
>
inline
NDT
nativeType
<
signed
char
>
()
{
return
NAT_SCHAR
;
}
template
<
>
inline
NDT
nativeType
<
unsigned
char
>
()
{
return
NAT_UCHAR
;
}
...
...
@@ -153,8 +137,10 @@ inline int ndt2size (NDT type)
case
NAT_LONGDOUBLE
:
return
sizeof
(
long
double
);
case
NAT_BOOL
:
return
sizeof
(
bool
);
default:
my
fail
(
"ndt2size: unsupported data type"
);
MR_
fail
(
"ndt2size: unsupported data type"
);
}
}
}}
#endif
src/mr_util/error_handling.h
View file @
52df59d3
#ifndef MRBASE_ERROR_HANDLING_H
#define MRBASE_ERROR_HANDLING_H
/*
* This file is part of the MR utility library.
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this code; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Copyright (C) 2019 Max-Planck-Society
Author: Martin Reinecke */
#ifndef MRUTIL_ERROR_HANDLING_H
#define MRUTIL_ERROR_HANDLING_H
#include
<iostream>
#include
<cstdlib>
namespace
mr
{
namespace
error_handling
{
namespace
detail
{
#if defined (__GNUC__)
#define
LOC_
CodeLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define
MR_ERROR_HANDLING_LOC_ ::mr::error_handling::detail::
CodeLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__)
#else
#define
LOC_
CodeLocation(__FILE__, __LINE__)
#define
MRERROR_HANDLING_LOC_ ::mr::error_handling::detail::
CodeLocation(__FILE__, __LINE__)
#endif
#define
my
fail(...) \
#define
MR_
fail(...) \
do { \
if (!abort_in_progress__) \
if (!
::mr::error_handling::detail::
abort_in_progress__) \
{ \
abort_in_progress__ = true; \
streamDump__(std::cerr, LOC_, "\n", ##__VA_ARGS__, "\n"); \
killjob__(); \
::mr::error_handling::detail::
abort_in_progress__ = true; \
::mr::error_handling::detail::
streamDump__(
::
std::cerr,
MR_ERROR_HANDLING_
LOC_, "\n", ##__VA_ARGS__, "\n"); \
::mr::error_handling::detail::
killjob__(); \
} \
std::exit(1); \
::
std::exit(1); \
} while(0)
#define
my
assert(cond,...) \
#define
MR_
assert(cond,...) \
do { \
if (cond); \
else {
my
fail("Assertion failure\n", ##__VA_ARGS__); } \
else {
MR_
fail("Assertion failure\n", ##__VA_ARGS__); } \
} while(0)
// to be replaced with std::source_location once available
...
...
@@ -38,10 +65,10 @@ class CodeLocation
CodeLocation
(
const
char
*
file_
,
int
line_
,
const
char
*
func_
=
nullptr
)
:
file
(
file_
),
func
(
func_
),
line
(
line_
)
{}
std
::
ostream
&
print
(
std
::
ostream
&
os
)
const
;
::
std
::
ostream
&
print
(
::
std
::
ostream
&
os
)
const
;
};
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
CodeLocation
&
loc
)
inline
::
std
::
ostream
&
operator
<<
(
::
std
::
ostream
&
os
,
const
CodeLocation
&
loc
)
{
return
loc
.
print
(
os
);
}
extern
bool
abort_in_progress__
;
...
...
@@ -49,11 +76,11 @@ void killjob__();
#if 1
template
<
typename
T
>
inline
void
streamDump__
(
std
::
ostream
&
os
,
const
T
&
value
)
inline
void
streamDump__
(
::
std
::
ostream
&
os
,
const
T
&
value
)
{
os
<<
value
;
}
template
<
typename
T
,
typename
...
Args
>
inline
void
streamDump__
(
std
::
ostream
&
os
,
const
T
&
value
,
inline
void
streamDump__
(
::
std
::
ostream
&
os
,
const
T
&
value
,
const
Args
&
...
args
)
{
os
<<
value
;
...
...
@@ -62,8 +89,10 @@ inline void streamDump__(std::ostream &os, const T& value,
#else
// hyper-elegant C++2017 version
template
<
typename
...
Args
>
inline
void
streamDump__
(
std
::
ostream
&
os
,
Args
&&
...
args
)
inline
void
streamDump__
(
::
std
::
ostream
&
os
,
Args
&&
...
args
)
{
(
os
<<
...
<<
args
);
}
#endif
}}}
#endif
src/mr_util/string_utils.h
View file @
52df59d3
...
...
@@ -29,12 +29,15 @@
* \author Martin Reinecke
*/
#ifndef MR
BASE
_STRING_UTILS_H
#define MR
BASE
_STRING_UTILS_H
#ifndef MR
UTIL
_STRING_UTILS_H
#define MR
UTIL
_STRING_UTILS_H
#include
<vector>
#include
<map>
#include
"datatypes.h"
namespace
mr
{
namespace
string_utils
{
/*! \defgroup stringutilsgroup String handling helper functions */
/*! \{ */
...
...
@@ -53,7 +56,7 @@ template<> std::string dataToString (const long double &x);
/*! Returns a string containing the text representation of \a x, padded
with leading zeroes to \a width characters. */
std
::
string
intToString
(
int64
x
,
t
size
width
);
std
::
string
intToString
(
std
::
int64
_t
x
,
std
::
size
_t
width
);
/*! Reads a value of a given datatype from a string */
template
<
typename
T
>
void
stringToData
(
const
std
::
string
&
x
,
T
&
value
);
...
...
@@ -120,4 +123,6 @@ void parse_words_from_file (const std::string &filename,
/*! \} */
}}
#endif
src/mr_util/system.h
View file @
52df59d3
#ifndef MR
BASE
_SYSTEM_H
#define MR
BASE
_SYSTEM_H
#ifndef MR
UTIL
_SYSTEM_H
#define MR
UTIL
_SYSTEM_H
#include
<string>
#include
"datatypes.h"
#include
<cstdlib>
tsize
getProcessInfo
(
const
std
::
string
&
quantity
);
tsize
getMemInfo
(
const
std
::
string
&
quantity
);
tsize
usable_memory
();
namespace
mr
{
namespace
system
{
std
::
size_t
getProcessInfo
(
const
std
::
string
&
quantity
);
std
::
size_t
getMemInfo
(
const
std
::
string
&
quantity
);
std
::
size_t
usable_memory
();
}}
#endif
src/mr_util/timers.h
View file @
52df59d3
#ifndef MR
BASE
_TIMERS_H
#define MR
BASE
_TIMERS_H
#ifndef MR
UTIL
_TIMERS_H
#define MR
UTIL
_TIMERS_H
#include
<chrono>
#include
<string>
#include
<map>
#include
"error_handling.h"
#include
"string_utils.h"
#include
"mr_util/error_handling.h"
#include
"mr_util/string_utils.h"
namespace
mr
{
namespace
timers
{
class
SimpleTimer
{
...
...
@@ -65,7 +69,7 @@ class TimerHierarchy
auto
it
=
curnode
->
child
.
find
(
name
);
if
(
it
==
curnode
->
child
.
end
())
{
my
assert
(
name
.
find
(
':'
)
==
std
::
string
::
npos
,
"reserved character"
);
MR_
assert
(
name
.
find
(
':'
)
==
std
::
string
::
npos
,
"reserved character"
);
it
=
curnode
->
child
.
insert
(
make_pair
(
name
,
tstack_node
(
curnode
))).
first
;
}
curnode
=&
(
it
->
second
);
...
...
@@ -83,7 +87,7 @@ class TimerHierarchy
{
adjust_time
();
curnode
=
curnode
->
parent
;
my
assert
(
curnode
!=
nullptr
,
"tried to pop from empty timer stack"
);
MR_
assert
(
curnode
!=
nullptr
,
"tried to pop from empty timer stack"
);
}
void
poppush
(
const
std
::
string
&
name
)
{
...
...
@@ -99,4 +103,6 @@ class TimerHierarchy
}
};
}}
#endif
src/string_utils.cc
View file @
52df59d3
...
...
@@ -37,10 +37,18 @@
#include
<string>
#include
<cstring>
#include
<cctype>
#include
"string_utils.h"
#include
"mr_util/string_utils.h"
#include
"mr_util/error_handling.h"
#include
"mr_util/datatypes.h"
using
namespace
std
;
namespace
mr
{
namespace
string_utils
{
using
namespace
mr
::
datatypes
;
string
trim
(
const
string
&
orig
)
{
string
::
size_type
p1
=
orig
.
find_first_not_of
(
"
\t
"
);
...
...
@@ -90,13 +98,13 @@ template string dataToString (const unsigned long &x);
template
string
dataToString
(
const
long
long
&
x
);
template
string
dataToString
(
const
unsigned
long
long
&
x
);
string
intToString
(
int64
x
,
t
size
width
)
string
intToString
(
int64
_t
x
,
size
_t
width
)
{
ostringstream
strstrm
;
(
x
>=
0
)
?
strstrm
<<
setw
(
width
)
<<
setfill
(
'0'
)
<<
x
:
strstrm
<<
"-"
<<
setw
(
width
-
1
)
<<
setfill
(
'0'
)
<<
-
x
;
string
res
=
strstrm
.
str
();
my
assert
(
res
.
size
()
==
width
,
"number too large"
);
MR_
assert
(
res
.
size
()
==
width
,
"number too large"
);
return
trim
(
res
);
}
...
...
@@ -104,12 +112,12 @@ namespace {
void
end_stringToData
(
const
string
&
x
,
const
char
*
tn
,
istringstream
&
strstrm
)
{
my
assert
(
strstrm
,
MR_
assert
(
strstrm
,
"conversion error in stringToData<"
,
tn
,
">('"
,
x
,
"')"
);
string
rest
;
strstrm
>>
rest
;
// rest=trim(rest);
my
assert
(
rest
.
length
()
==
0
,
MR_
assert
(
rest
.
length
()
==
0
,
"conversion error in stringToData<"
,
tn
,
">('"
,
x
,
"')"
);
}
...
...
@@ -129,11 +137,11 @@ template<> void stringToData (const string &x, bool &value)
{
const
char
*
fval
[]
=
{
"f"
,
"n"
,
"false"
,
".false."
};
const
char
*
tval
[]
=
{
"t"
,
"y"
,
"true"
,
".true."
};
for
(
t
size
i
=
0
;
i
<
sizeof
(
fval
)
/
sizeof
(
fval
[
0
]);
++
i
)
for
(
size
_t
i
=
0
;
i
<
sizeof
(
fval
)
/
sizeof
(
fval
[
0
]);
++
i
)
if
(
equal_nocase
(
x
,
fval
[
i
]))
{
value
=
false
;
return
;
}
for
(
t
size
i
=
0
;
i
<
sizeof
(
tval
)
/
sizeof
(
tval
[
0
]);
++
i
)
for
(
size
_t
i
=
0
;
i
<
sizeof
(
tval
)
/
sizeof
(
tval
[
0
]);
++
i
)
if
(
equal_nocase
(
x
,
tval
[
i
]))
{
value
=
true
;
return
;
}
my
fail
(
"conversion error in stringToData<bool>("
,
x
,
")"
);
MR_
fail
(
"conversion error in stringToData<bool>("
,
x
,
")"
);
}
template
void
stringToData
(
const
string
&
x
,
signed
char
&
value
);
...
...
@@ -153,16 +161,16 @@ template void stringToData (const string &x, long double &value);
bool
equal_nocase
(
const
string
&
a
,
const
string
&
b
)
{
if
(
a
.
size
()
!=
b
.
size
())
return
false
;
for
(
t
size
m
=
0
;
m
<
a
.
size
();
++
m
)
if
(
tolower
(
a
[
m
])
!=
tolower
(
b
[
m
]))
return
false
;
for
(
size
_t
m
=
0
;
m
<
a
.
size
();
++
m
)
if
(
std
::
tolower
(
a
[
m
])
!=
std
::
tolower
(
b
[
m
]))
return
false
;
return
true
;
}
string
tolower
(
const
string
&
input
)
{
string
result
=
input
;
for
(
t
size
m
=
0
;
m
<
result
.
size
();
++
m
)
result
[
m
]
=
char
(
tolower
(
result
[
m
]));
for
(
size
_t
m
=
0
;
m
<
result
.
size
();
++
m
)
result
[
m
]
=
char
(
std
::
tolower
(
result
[
m
]));
return
result
;
}
...
...
@@ -171,7 +179,7 @@ void parse_file (const string &filename, map<string,string> &dict)
int
lineno
=
0
;
dict
.
clear
();
ifstream
inp
(
filename
.
c_str
());
my
assert
(
inp
,
"Could not open parameter file '"
,
filename
,
"'."
);
MR_
assert
(
inp
,
"Could not open parameter file '"
,
filename
,
"'."
);
while
(
inp
)
{
string
line
;
...
...
@@ -221,14 +229,14 @@ void parse_cmdline_classic (int argc, const char **argv,
const
vector
<
string
>
&
leading_args
,
map
<
string
,
string
>
&
dict
)
{
dict
.
clear
();
my
assert
(
t
size
(
argc
)
>
leading_args
.
size
(),
"not enough arguments"
);
for
(
t
size
i
=
0
;
i
<
leading_args
.
size
();
++
i
)
MR_
assert
(
size
_t
(
argc
)
>
leading_args
.
size
(),
"not enough arguments"
);
for
(
size
_t
i
=
0
;
i
<
leading_args
.
size
();
++
i
)
dict
[
leading_args
[
i
]]
=
argv
[
i
+
1
];
int
curarg
=
leading_args
.
size
()
+
1
;
while
(
curarg
<
argc
)
{
string
param
=
argv
[
curarg
];
my
assert
(
isParam
(
param
),
"unrecognized command line format"
);
MR_
assert
(
isParam
(
param
),
"unrecognized command line format"
);
if
((
curarg
==
argc
-
1
)
||
isParam
(
argv
[
curarg
+
1
]))
{
dict
[
param
.
substr
(
1
)]
=
"true"
;
...
...
@@ -250,8 +258,8 @@ void parse_cmdline_equalsign (int argc, const char **argv,
const
vector
<
string
>
&
leading_args
,
map
<
string
,
string
>
&
dict
)
{
dict
.
clear
();
my
assert
(
t
size
(
argc
)
>
leading_args
.
size
(),
"not enough arguments"
);
for
(
t
size
i
=
0
;
i
<
leading_args
.
size
();
++
i
)
MR_
assert
(
size
_t
(
argc
)
>
leading_args
.
size
(),
"not enough arguments"
);
for
(
size
_t
i
=
0
;
i
<
leading_args
.
size
();
++
i
)
dict
[
leading_args
[
i
]]
=
argv
[
i
+
1
];
for
(
int
i
=
leading_args
.
size
()
+
1
;
i
<
argc
;
++
i
)
{
...
...
@@ -292,7 +300,7 @@ template<typename T> void split (istream &stream, vector<T> &list)
{
string
word
;
stream
>>
word
;
my
assert
(
stream
||
stream
.
eof
(),
MR_
assert
(
stream
||
stream
.
eof
(),
"error while splitting stream into "
,
type2typename
<
T
>
(),
" components"
);
if
(
stream
)
list
.
push_back
(
stringToData
<
T
>
(
word
));
}
...
...
@@ -325,7 +333,7 @@ void parse_words_from_file (const string &filename, vector<string> &words)
{
words
.
clear
();
ifstream
inp
(
filename
.
c_str
());
my
assert
(
inp
,
"Could not open file '"
,
filename
,
"'."
);
MR_
assert
(
inp
,
"Could not open file '"
,
filename
,
"'."
);
while
(
inp
)
{
string
word
;
...
...
@@ -334,3 +342,5 @@ void parse_words_from_file (const string &filename, vector<string> &words)
if
(
word
!=
""
)
words
.
push_back
(
word
);
}
}
}}
src/system.cc
View file @
52df59d3
...
...
@@ -3,10 +3,16 @@
#include
<fstream>
#include
<sstream>
#include
"system.h"
#include
"string_utils.h"
#include
"mr_util/error_handling.h"
#include
"mr_util/system.h"
#include
"mr_util/string_utils.h"
using
namespace
std
;
using
namespace
mr
::
string_utils
;
namespace
mr
{
namespace
system
{
namespace
{
...
...
@@ -23,28 +29,30 @@ template<typename T> T find(const string &s, const string &pattern)
regex
re
(
pattern
);
sregex_iterator
it
(
s
.
begin
(),
s
.
end
(),
re
);
sregex_iterator
it_end
;
my
assert
(
it
!=
it_end
,
"did not find pattern '"
,
pattern
,
"'"
);
MR_
assert
(
it
!=
it_end
,
"did not find pattern '"
,
pattern
,
"'"
);
return
stringToData
<
T
>
(
it
->
str
(
1
));
}
}
// unnamed namespace
t
size
getProcessInfo
(
const
string
&
quantity
)
size
_t
getProcessInfo
(
const
string
&
quantity
)
{
string
text
=
fileToString
(
"/proc/self/status"
);
return
find
<
t
size
>
(
text
,
quantity
+
R"(:\s+(\d+))"
);
return
find
<
size
_t
>
(
text
,
quantity
+
R"(:\s+(\d+))"
);
}
t
size
getMemInfo
(
const
string
&
quantity
)
size
_t
getMemInfo
(
const
string
&
quantity
)
{
string
text
=
fileToString
(
"/proc/meminfo"
);
return
find
<
t
size
>
(
text
,
quantity
+
R"(:\s+(\d+) kB)"
);
return
find
<
size
_t
>
(
text
,
quantity
+
R"(:\s+(\d+) kB)"
);
}
t
size
usable_memory
()
size
_t
usable_memory
()
{
string
text
=
fileToString
(
"/proc/meminfo"
);
t
size
MemTotal
=
find
<
t
size
>
(
text
,
R"(MemTotal:\s+(\d+) kB)"
);
t
size
Committed
=
find
<
t
size
>
(
text
,
R"(Committed_AS:\s+(\d+) kB)"
);
size
_t
MemTotal
=
find
<
size
_t
>
(
text
,
R"(MemTotal:\s+(\d+) kB)"
);
size
_t
Committed
=
find
<
size
_t
>
(
text
,
R"(Committed_AS:\s+(\d+) kB)"
);
return
MemTotal
-
Committed
;
}
}}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment