Userspace Resource Manager
Loading...
Searching...
No Matches
AuxRoutines.h
1// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef AUX_ROUTINES_H
5#define AUX_ROUTINES_H
6
7#include <mutex>
8#include <queue>
9#include <memory>
10#include <string>
11#include <cstring>
12#include <sstream>
13#include <fstream>
14#include <unistd.h>
15#include <getopt.h>
16#include <dirent.h>
17#include <algorithm>
18#include <unordered_set>
19
20#include "Logger.h"
21#include "ClientEndpoint.h"
22#include "UrmSettings.h"
23#include "SafeOps.h"
24
25class AuxRoutines {
26private:
27 static std::mutex handleGenLock;
28
29public:
30 static std::string readFromFile(const std::string& fileName);
31 static void writeToFile(const std::string& fileName, const std::string& value);
32 static void deleteFile(const std::string& fileName);
33 static void writeSysFsDefaults();
34 static int8_t fileExists(const std::string& filePath);
35 static int8_t fileWritable(const std::string& filePath);
36 static std::string getMachineName();
37
38 static int8_t isNumericString(const std::string& str);
39 static void cpuMaskToHex(uint64_t mask, std::string& result);
40 static pid_t fetchPid(const std::string& processName);
41 static int32_t fetchComm(pid_t pid, std::string &comm);
42
43 static int64_t generateUniqueHandle();
44 static int64_t getCurrentTimeInMilliseconds();
45 static void toLowerCase(std::string& str);
46};
47
48// Following are some client-lib centric utilities
49class FlatBuffEncoder {
50private:
51 char* mBuffer;
52 void* mCurPtr;
53 int32_t mRunningIndex;
54
55public:
56 FlatBuffEncoder();
57
58 template <typename T>
59 FlatBuffEncoder& append(T val) {
60 if(this->mRunningIndex == -1 || this->mBuffer == nullptr) {
61 return *this;
62 }
63
64 if(this->mRunningIndex + sizeof(T) < REQ_BUFFER_SIZE) {
65 T* tPtr = (T*)(this->mCurPtr);
66 try {
67 ASSIGN_AND_INCR(tPtr, val);
68 this->mRunningIndex += sizeof(T);
69 this->mCurPtr = reinterpret_cast<char*>(tPtr);
70
71 } catch(const std::exception& e) {
72 this->mRunningIndex = -1;
73 }
74 } else {
75 // Prevent further updates on the current buffer
76 this->mRunningIndex = REQ_BUFFER_SIZE;
77 }
78
79 return *this;
80 }
81
82 FlatBuffEncoder appendString(const char* valStr);
83
84 void setBuf(char* buffer);
85 int8_t isBufSane();
86};
87
88class ConnectionManager {
89private:
90 std::shared_ptr<ClientEndpoint> connection;
91
92public:
93 ConnectionManager(std::shared_ptr<ClientEndpoint> connection) {
94 this->connection = connection;
95 }
96
97 ~ConnectionManager() {
98 if(this->connection != nullptr) {
99 this->connection->closeConnection();
100 }
101 }
102};
103
104class MinLRUCache {
105private:
106 size_t mMaxSize;
107 std::unordered_set<int64_t> mDataSet;
108 std::queue<int64_t> mRecencyQueue;
109
110public:
111 MinLRUCache(int32_t maxSize = 30);
112
113 void insert(int64_t data);
114 int8_t isPresent(int64_t data);
115};
116
117#endif