utils: some more reshuffling/grouping and formatting

This commit is contained in:
Peter Keresztes Schmidt 2021-04-04 00:51:38 +02:00
parent 545f0dbb96
commit b5f3682d4e
6 changed files with 106 additions and 108 deletions

View File

@ -1738,7 +1738,7 @@ void Image::Blend( const Image &image, int transparency ) {
#ifdef ZM_IMAGE_PROFILING #ifdef ZM_IMAGE_PROFILING
clock_gettime(CLOCK_THREAD_CPUTIME_ID,&end); clock_gettime(CLOCK_THREAD_CPUTIME_ID,&end);
timespec_diff(&start,&end,&diff); TimespecDiff(&start,&end,&diff);
executetime = (1000000000ull * diff.tv_sec) + diff.tv_nsec; executetime = (1000000000ull * diff.tv_sec) + diff.tv_nsec;
milpixels = (unsigned long)((long double)size)/((((long double)executetime)/1000)); milpixels = (unsigned long)((long double)size)/((((long double)executetime)/1000));
@ -1902,7 +1902,7 @@ void Image::Delta(const Image &image, Image* targetimage) const {
#ifdef ZM_IMAGE_PROFILING #ifdef ZM_IMAGE_PROFILING
clock_gettime(CLOCK_THREAD_CPUTIME_ID,&end); clock_gettime(CLOCK_THREAD_CPUTIME_ID,&end);
timespec_diff(&start,&end,&diff); TimespecDiff(&start,&end,&diff);
executetime = (1000000000ull * diff.tv_sec) + diff.tv_nsec; executetime = (1000000000ull * diff.tv_sec) + diff.tv_nsec;
milpixels = (unsigned long)((long double)pixels)/((((long double)executetime)/1000)); milpixels = (unsigned long)((long double)pixels)/((((long double)executetime)/1000));

View File

@ -134,7 +134,7 @@ int main(int argc, char *argv[]) {
exit(-1); exit(-1);
} }
hwcaps_detect(); HwCapsDetect();
std::string where = "`Function` != 'None' AND `RTSPServer` != false"; std::string where = "`Function` != 'None' AND `RTSPServer` != false";
if (staticConfig.SERVER_ID) if (staticConfig.SERVER_ID)

View File

@ -113,7 +113,7 @@ std::string Join(const StringVector &values, const std::string &delim) {
std::stringstream ss; std::stringstream ss;
for (size_t i = 0; i < values.size(); ++i) { for (size_t i = 0; i < values.size(); ++i) {
if ( i != 0 ) if (i != 0)
ss << delim; ss << delim;
ss << values[i]; ss << values[i];
} }
@ -167,36 +167,58 @@ std::string Base64Encode(const std::string &str) {
return outString; return outString;
} }
void TimespecDiff(struct timespec *start, struct timespec *end, struct timespec *diff) {
if (((end->tv_nsec) - (start->tv_nsec)) < 0) {
diff->tv_sec = end->tv_sec - start->tv_sec - 1;
diff->tv_nsec = 1000000000 + end->tv_nsec - start->tv_nsec;
} else {
diff->tv_sec = end->tv_sec - start->tv_sec;
diff->tv_nsec = end->tv_nsec - start->tv_nsec;
}
}
std::string TimevalToString(timeval tv) {
tm now = {};
std::array<char, 26> tm_buf = {};
localtime_r(&tv.tv_sec, &now);
size_t tm_buf_len = strftime(tm_buf.data(), tm_buf.size(), "%Y-%m-%d %H:%M:%S", &now);
if (tm_buf_len == 0) {
return "";
}
return stringtf("%s.%06ld", tm_buf.data(), tv.tv_usec);
}
/* Detect special hardware features, such as SIMD instruction sets */ /* Detect special hardware features, such as SIMD instruction sets */
void hwcaps_detect() { void HwCapsDetect() {
neonversion = 0; neonversion = 0;
sse_version = 0; sse_version = 0;
#if (defined(__i386__) || defined(__x86_64__)) #if (defined(__i386__) || defined(__x86_64__))
__builtin_cpu_init(); __builtin_cpu_init();
if (__builtin_cpu_supports("avx2")) {
if ( __builtin_cpu_supports("avx2") ) {
sse_version = 52; /* AVX2 */ sse_version = 52; /* AVX2 */
Debug(1, "Detected a x86\\x86-64 processor with AVX2"); Debug(1, "Detected a x86\\x86-64 processor with AVX2");
} else if ( __builtin_cpu_supports("avx") ) { } else if (__builtin_cpu_supports("avx")) {
sse_version = 51; /* AVX */ sse_version = 51; /* AVX */
Debug(1, "Detected a x86\\x86-64 processor with AVX"); Debug(1, "Detected a x86\\x86-64 processor with AVX");
} else if ( __builtin_cpu_supports("sse4.2") ) { } else if (__builtin_cpu_supports("sse4.2")) {
sse_version = 42; /* SSE4.2 */ sse_version = 42; /* SSE4.2 */
Debug(1, "Detected a x86\\x86-64 processor with SSE4.2"); Debug(1, "Detected a x86\\x86-64 processor with SSE4.2");
} else if ( __builtin_cpu_supports("sse4.1") ) { } else if (__builtin_cpu_supports("sse4.1")) {
sse_version = 41; /* SSE4.1 */ sse_version = 41; /* SSE4.1 */
Debug(1, "Detected a x86\\x86-64 processor with SSE4.1"); Debug(1, "Detected a x86\\x86-64 processor with SSE4.1");
} else if ( __builtin_cpu_supports("ssse3") ) { } else if (__builtin_cpu_supports("ssse3")) {
sse_version = 35; /* SSSE3 */ sse_version = 35; /* SSSE3 */
Debug(1,"Detected a x86\\x86-64 processor with SSSE3"); Debug(1, "Detected a x86\\x86-64 processor with SSSE3");
} else if ( __builtin_cpu_supports("sse3") ) { } else if (__builtin_cpu_supports("sse3")) {
sse_version = 30; /* SSE3 */ sse_version = 30; /* SSE3 */
Debug(1, "Detected a x86\\x86-64 processor with SSE3"); Debug(1, "Detected a x86\\x86-64 processor with SSE3");
} else if ( __builtin_cpu_supports("sse2") ) { } else if (__builtin_cpu_supports("sse2")) {
sse_version = 20; /* SSE2 */ sse_version = 20; /* SSE2 */
Debug(1, "Detected a x86\\x86-64 processor with SSE2"); Debug(1, "Detected a x86\\x86-64 processor with SSE2");
} else if ( __builtin_cpu_supports("sse") ) { } else if (__builtin_cpu_supports("sse")) {
sse_version = 10; /* SSE */ sse_version = 10; /* SSE */
Debug(1, "Detected a x86\\x86-64 processor with SSE"); Debug(1, "Detected a x86\\x86-64 processor with SSE");
} else { } else {
@ -227,13 +249,13 @@ void hwcaps_detect() {
/* SSE2 aligned memory copy. Useful for big copying of aligned memory like image buffers in ZM */ /* SSE2 aligned memory copy. Useful for big copying of aligned memory like image buffers in ZM */
/* For platforms without SSE2 we will use standard x86 asm memcpy or glibc's memcpy() */ /* For platforms without SSE2 we will use standard x86 asm memcpy or glibc's memcpy() */
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
__attribute__((noinline,__target__("sse2"))) __attribute__((noinline, __target__("sse2")))
#endif #endif
void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes) { void *sse2_aligned_memcpy(void *dest, const void *src, size_t bytes) {
#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE)) #if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
if(bytes > 128) { if (bytes > 128) {
unsigned int remainder = bytes % 128; unsigned int remainder = bytes % 128;
const uint8_t* lastsrc = (uint8_t*)src + (bytes - remainder); const uint8_t *lastsrc = (uint8_t *) src + (bytes - remainder);
__asm__ __volatile__( __asm__ __volatile__(
"sse2_copy_iter:\n\t" "sse2_copy_iter:\n\t"
@ -269,7 +291,7 @@ void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes) {
} else { } else {
/* Standard memcpy */ /* Standard memcpy */
__asm__ __volatile__("cld; rep movsb" :: "S"(src), "D"(dest), "c"(bytes) : "cc", "memory"); __asm__ __volatile__("cld; rep movsb"::"S"(src), "D"(dest), "c"(bytes) : "cc", "memory");
} }
#else #else
/* Non x86\x86-64 platform, use memcpy */ /* Non x86\x86-64 platform, use memcpy */
@ -278,30 +300,21 @@ void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes) {
return dest; return dest;
} }
void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff) { void touch(const char *pathname) {
if (((end->tv_nsec)-(start->tv_nsec))<0) { int fd = open(pathname, O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK, 0666);
diff->tv_sec = end->tv_sec-start->tv_sec-1; if (fd < 0) {
diff->tv_nsec = 1000000000+end->tv_nsec-start->tv_nsec; // Couldn't open that path.
} else { Error("Couldn't open() path %s in touch", pathname);
diff->tv_sec = end->tv_sec-start->tv_sec; return;
diff->tv_nsec = end->tv_nsec-start->tv_nsec; }
int rc = utimensat(AT_FDCWD, pathname, nullptr, 0);
if (rc) {
Error("Couldn't utimensat() path %s in touch", pathname);
return;
} }
} }
std::string TimevalToString(timeval tv) { std::string UriDecode(const std::string &encoded) {
tm now = {};
std::array<char, 26> tm_buf = {};
localtime_r(&tv.tv_sec, &now);
size_t tm_buf_len = strftime(tm_buf.data(), tm_buf.size(), "%Y-%m-%d %H:%M:%S", &now);
if (tm_buf_len == 0) {
return "";
}
return stringtf("%s.%06ld", tm_buf.data(), tv.tv_usec);
}
std::string UriDecode( const std::string &encoded ) {
char a, b; char a, b;
const char *src = encoded.c_str(); const char *src = encoded.c_str();
std::string retbuf; std::string retbuf;
@ -309,19 +322,19 @@ std::string UriDecode( const std::string &encoded ) {
while (*src) { while (*src) {
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) { if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
if (a >= 'a') if (a >= 'a')
a -= 'a'-'A'; a -= 'a' - 'A';
if (a >= 'A') if (a >= 'A')
a -= ('A' - 10); a -= ('A' - 10);
else else
a -= '0'; a -= '0';
if (b >= 'a') if (b >= 'a')
b -= 'a'-'A'; b -= 'a' - 'A';
if (b >= 'A') if (b >= 'A')
b -= ('A' - 10); b -= ('A' - 10);
else else
b -= '0'; b -= '0';
retbuf.push_back(16*a+b); retbuf.push_back(16 * a + b);
src+=3; src += 3;
} else if (*src == '+') { } else if (*src == '+') {
retbuf.push_back(' '); retbuf.push_back(' ');
src++; src++;
@ -332,27 +345,7 @@ std::string UriDecode( const std::string &encoded ) {
return retbuf; return retbuf;
} }
void touch(const char *pathname) {
int fd = open(pathname,
O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK,
0666);
if ( fd < 0 ) {
// Couldn't open that path.
Error("Couldn't open() path %s in touch", pathname);
return;
}
int rc = utimensat(AT_FDCWD,
pathname,
nullptr,
0);
if ( rc ) {
Error("Couldn't utimensat() path %s in touch", pathname);
return;
}
}
QueryString::QueryString(std::istream &input) { QueryString::QueryString(std::istream &input) {
while (!input.eof() && input.peek() > 0) { while (!input.eof() && input.peek() > 0) {
//Should eat "param1=" //Should eat "param1="
auto name = parseName(input); auto name = parseName(input);
@ -361,20 +354,20 @@ QueryString::QueryString(std::istream &input) {
auto foundItr = parameters_.find(name); auto foundItr = parameters_.find(name);
if (foundItr == parameters_.end()) { if (foundItr == parameters_.end()) {
auto newParam = ZM::make_unique<QueryParameter>(name); std::unique_ptr<QueryParameter> newParam = ZM::make_unique<QueryParameter>(name);
if (value.size() > 0) { if (!value.empty()) {
newParam->addValue(value); newParam->addValue(value);
} }
parameters_.emplace(name, std::move(newParam)).first; parameters_.emplace(name, std::move(newParam));
} else { } else {
foundItr->second->addValue(value); foundItr->second->addValue(value);
} }
} // end while not the end }
} }
std::vector<std::string> QueryString::names() const { std::vector<std::string> QueryString::names() const {
std::vector<std::string> names; std::vector<std::string> names;
for (auto const& pair : parameters_) for (auto const &pair : parameters_)
names.push_back(pair.second->name()); names.push_back(pair.second->name());
return names; return names;
@ -383,31 +376,35 @@ std::vector<std::string> QueryString::names() const {
const QueryParameter *QueryString::get(const std::string &name) const { const QueryParameter *QueryString::get(const std::string &name) const {
auto itr = parameters_.find(name); auto itr = parameters_.find(name);
return itr == parameters_.end() ? nullptr : itr->second.get(); return itr == parameters_.end() ? nullptr : itr->second.get();
}; }
std::string QueryString::parseName(std::istream &input) { std::string QueryString::parseName(std::istream &input) {
std::string name = ""; std::string name;
while (!input.eof() && input.peek() != '=') while (!input.eof() && input.peek() != '=') {
name.push_back(input.get()); name.push_back(input.get());
}
//Eat the '=' //Eat the '='
if (!input.eof()) input.get(); if (!input.eof()) {
input.get();
}
return name; return name;
} }
std::string QueryString::parseValue(std::istream &input) { std::string QueryString::parseValue(std::istream &input) {
std::string urlEncodedValue; std::string url_encoded_value;
int c = input.get(); int c = input.get();
while (c > 0 && c != '&') { while (c > 0 && c != '&') {
urlEncodedValue.push_back(c); url_encoded_value.push_back(c);
c = input.get(); c = input.get();
} }
if (urlEncodedValue.size() == 0) if (url_encoded_value.empty()) {
return ""; return "";
}
return UriDecode(urlEncodedValue); return UriDecode(url_encoded_value);
} }

View File

@ -60,16 +60,15 @@ std::string stringtf(const std::string &format, Args... args) {
std::string Base64Encode(const std::string &str); std::string Base64Encode(const std::string &str);
void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes); void TimespecDiff(timespec *start, timespec *end, timespec *diff);
void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff); std::string TimevalToString(timeval tv);
void hwcaps_detect();
extern unsigned int sse_version; extern unsigned int sse_version;
extern unsigned int neonversion; extern unsigned int neonversion;
void HwCapsDetect();
void *sse2_aligned_memcpy(void *dest, const void *src, size_t bytes);
std::string TimevalToString(timeval tv); void touch(const char *pathname);
std::string UriDecode( const std::string &encoded );
void touch( const char *pathname );
namespace ZM { namespace ZM {
//! std::make_unique implementation (TODO: remove this once C++14 is supported) //! std::make_unique implementation (TODO: remove this once C++14 is supported)
@ -99,39 +98,41 @@ typedef std::chrono::hours Hours;
typedef std::chrono::steady_clock::time_point TimePoint; typedef std::chrono::steady_clock::time_point TimePoint;
typedef std::chrono::system_clock::time_point SystemTimePoint; typedef std::chrono::system_clock::time_point SystemTimePoint;
std::string UriDecode(const std::string &encoded);
class QueryParameter { class QueryParameter {
public: public:
const std::string &name() const { return name_; } explicit QueryParameter(std::string name) : name_(std::move(name)) {}
const std::string &firstValue() const { return values_[0]; }
const std::vector<std::string> &values() const { return values_; } const std::string &name() const { return name_; }
size_t size() const { return values_.size(); } const std::string &firstValue() const { return values_[0]; }
QueryParameter(std::string name) : name_(std::move(name)) { } const std::vector<std::string> &values() const { return values_; }
size_t size() const { return values_.size(); }
template<class T> void addValue(T&& value) { values_.emplace_back(std::forward<T>(value)); } template<class T>
private: void addValue(T &&value) { values_.emplace_back(std::forward<T>(value)); }
std::string name_; private:
std::vector<std::string> values_; std::string name_;
std::vector<std::string> values_;
}; };
class QueryString { class QueryString {
public: public:
QueryString(std::istream &input); explicit QueryString(std::istream &input);
size_t size() const { return parameters_.size(); } size_t size() const { return parameters_.size(); }
bool has(const char *name) const { return parameters_.find(std::string(name)) != parameters_.end(); } bool has(const char *name) const { return parameters_.find(std::string(name)) != parameters_.end(); }
std::vector<std::string> names() const; std::vector<std::string> names() const;
const QueryParameter *get(const std::string &name) const; const QueryParameter *get(const std::string &name) const;
const QueryParameter *get(const char* name) const { return get(std::string(name)); }; const QueryParameter *get(const char *name) const { return get(std::string(name)); };
private: private:
static std::string parseName(std::istream &input);
static std::string parseValue(std::istream &input);
static std::string parseName(std::istream &input); std::map<std::string, std::unique_ptr<QueryParameter>> parameters_;
static std::string parseValue(std::istream &input);
std::map<std::string, std::unique_ptr<QueryParameter>> parameters_;
}; };
#endif // ZM_UTILS_H #endif // ZM_UTILS_H

View File

@ -189,7 +189,7 @@ int main(int argc, char *argv[]) {
zmLoadDBConfig(); zmLoadDBConfig();
logInit(log_id_string); logInit(log_id_string);
hwcaps_detect(); HwCapsDetect();
std::vector<std::shared_ptr<Monitor>> monitors; std::vector<std::shared_ptr<Monitor>> monitors;
#if ZM_HAS_V4L #if ZM_HAS_V4L

View File

@ -229,7 +229,7 @@ int main(int argc, const char *argv[], char **envp) {
user = nullptr; user = nullptr;
} // end if config.opt_use_auth } // end if config.opt_use_auth
hwcaps_detect(); HwCapsDetect();
zmSetDefaultTermHandler(); zmSetDefaultTermHandler();
zmSetDefaultDieHandler(); zmSetDefaultDieHandler();