Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower sleep times for rollinglog -f #6572

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions hyprctl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void intHandler(int sig) {
std::cout << "[hyprctl] SIGINT received, closing connection" << std::endl;
}

int rollingRead(const int socket) {
int rollingRead(const int socket, int rate) {
sigintReceived = false;
signal(SIGINT, intHandler);

Expand All @@ -132,13 +132,13 @@ int rollingRead(const int socket) {
buffer.fill('\0');
}

usleep(100000);
usleep(static_cast<int>((1.0 / rate) * 1000000));
}
close(socket);
return 0;
}

int request(std::string arg, int minArgs = 0, bool needRoll = false) {
int request(std::string arg, int minArgs = 0, bool needRoll = false, int rollRate = 20) {
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);

auto t = timeval{.tv_sec = 0, .tv_usec = 100000};
Expand Down Expand Up @@ -183,7 +183,7 @@ int request(std::string arg, int minArgs = 0, bool needRoll = false) {
}

if (needRoll)
return rollingRead(SERVERSOCKET);
return rollingRead(SERVERSOCKET, rollRate);

std::string reply = "";
char buffer[8192] = {0};
Expand Down Expand Up @@ -329,6 +329,7 @@ int main(int argc, char** argv) {
const auto ARGS = splitArgs(argc, argv);
bool json = false;
bool needRoll = false;
int rollRate = 20;
std::string overrideInstance = "";

for (std::size_t i = 0; i < ARGS.size(); ++i) {
Expand All @@ -351,6 +352,13 @@ int main(int argc, char** argv) {
} else if ((ARGS[i] == "-f" || ARGS[i] == "--follow") && !fullArgs.contains("f")) {
fullArgs += "f";
needRoll = true;
if (ARGS.size() >= 3)
try {
rollRate = std::stoi(ARGS[2]);
} catch (std::invalid_argument& e) {
log("invalid argument\n");
return 1;
}
} else if (ARGS[i] == "--batch") {
fullRequest = "--batch ";
} else if (ARGS[i] == "--instance" || ARGS[i] == "-i") {
Expand Down Expand Up @@ -475,7 +483,7 @@ int main(int argc, char** argv) {
else if (fullRequest.contains("/--help"))
std::cout << USAGE << std::endl;
else if (fullRequest.contains("/rollinglog") && needRoll)
exitStatus = request(fullRequest, 0, true);
exitStatus = request(fullRequest, 0, true, rollRate);
else {
exitStatus = request(fullRequest);
}
Expand Down
26 changes: 21 additions & 5 deletions src/debug/HyprCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,9 @@ std::string CHyprCtl::getReply(std::string request) {

std::string result = "";

if (request.starts_with("rollinglog"))
request = request.substr(0, request.find(" "));

// parse exact cmds first, then non-exact.
for (auto& cmd : m_vCommands) {
if (!cmd->exact)
Expand Down Expand Up @@ -1746,14 +1749,15 @@ bool successWrite(int fd, const std::string& data, bool needLog = true) {
return false;
}

void runWritingDebugLogThread(const int conn) {
void runWritingDebugLogThread(const int conn, int rate = 20) {
using namespace std::chrono_literals;
Debug::log(LOG, "In followlog thread, got connection, start writing: {}", conn);
//will be finished, when reading side close connection
std::thread([conn]() {
std::thread([conn, rate]() {
const auto RATEMS = std::chrono::milliseconds(static_cast<int>((1.0 / rate) * 1000));
while (Debug::RollingLogFollow::Get().IsRunning()) {
if (Debug::RollingLogFollow::Get().isEmpty(conn)) {
std::this_thread::sleep_for(1000ms);
phonetic112 marked this conversation as resolved.
Show resolved Hide resolved
std::this_thread::sleep_for(RATEMS);
continue;
}

Expand All @@ -1762,7 +1766,7 @@ void runWritingDebugLogThread(const int conn) {
// We cannot write, when connection is closed. So thread will successfully exit by itself
break;

std::this_thread::sleep_for(100ms);
std::this_thread::sleep_for(RATEMS);
}
close(conn);
Debug::RollingLogFollow::Get().StopFor(conn);
Expand Down Expand Up @@ -1821,7 +1825,19 @@ int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
if (isFollowUpRollingLogRequest(request)) {
Debug::log(LOG, "Followup rollinglog request received. Starting thread to write to socket.");
Debug::RollingLogFollow::Get().StartFor(ACCEPTEDCONNECTION);
runWritingDebugLogThread(ACCEPTEDCONNECTION);
std::istringstream iss(request);
std::string val;
if (iss >> val) {
if (iss >> val) {
try {
runWritingDebugLogThread(ACCEPTEDCONNECTION, std::stoi(val));
} catch (std::invalid_argument& e) { Debug::log(ERR, "Error in rollinglog request: {}", e.what()); }
} else {
runWritingDebugLogThread(ACCEPTEDCONNECTION);
}
} else {
Debug::log(ERR, "Invalid rollinglog request");
}
phonetic112 marked this conversation as resolved.
Show resolved Hide resolved
Debug::log(LOG, Debug::RollingLogFollow::Get().DebugInfo());
} else
close(ACCEPTEDCONNECTION);
Expand Down
Loading