diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-10-19 01:30:28 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-10-19 01:30:28 +0000 |
commit | 3a8319fd91dfa5ae83e8067e938deab66cd613a3 (patch) | |
tree | 7f580f87d79d8b14b746130cbdc583e763a1feb5 | |
parent | e6c70d8d1b55feb7e1a79624f37cda239821c997 (diff) | |
parent | 652dbe4565ec3b0e0d59be6c0ccb603197adee9f (diff) | |
download | dittosuite-3a8319fd91dfa5ae83e8067e938deab66cd613a3.tar.gz |
Snap for 10970117 from 652dbe4565ec3b0e0d59be6c0ccb603197adee9f to 24Q1-release
Change-Id: Ib4b92041dd9394a2000922dd1e4dd2379cab979c
-rw-r--r-- | dittobench.cpp | 3 | ||||
-rw-r--r-- | include/ditto/instruction.h | 4 | ||||
-rw-r--r-- | include/ditto/multithreading_utils.h | 2 | ||||
-rw-r--r-- | src/instruction.cpp | 11 | ||||
-rw-r--r-- | src/multiprocessing.cpp | 10 | ||||
-rw-r--r-- | src/multithreading_utils.cpp | 94 |
6 files changed, 119 insertions, 5 deletions
diff --git a/dittobench.cpp b/dittobench.cpp index 3b6fc63..6018eb4 100644 --- a/dittobench.cpp +++ b/dittobench.cpp @@ -19,6 +19,7 @@ #include <string> #include <ditto/arg_parser.h> +#include <ditto/instruction.h> #include <ditto/logger.h> #include <ditto/parser.h> #include <ditto/tracer.h> @@ -26,6 +27,8 @@ int main(int argc, char** argv) { dittosuite::Tracer tracer; dittosuite::CmdArguments arguments = dittosuite::ParseArguments(argc, argv); + dittosuite::Instruction::SetArgv(argv); + dittosuite::Instruction::SetArgc(argc); auto benchmark = dittosuite::Parser::GetParser().Parse(arguments.file_path, arguments.parameters); tracer.StartSession(std::move(benchmark)); diff --git a/include/ditto/instruction.h b/include/ditto/instruction.h index c4bdb09..d9fc568 100644 --- a/include/ditto/instruction.h +++ b/include/ditto/instruction.h @@ -53,6 +53,8 @@ class Instruction { virtual std::unique_ptr<Result> CollectResults(const std::string& prefix); static void SetAbsolutePathKey(int absolute_path_key); + static void SetArgv(char** argv); + static void SetArgc(int argc); protected: virtual void SetUpSingle(); @@ -68,6 +70,8 @@ class Instruction { std::string GetAbsolutePath(); static int absolute_path_key_; + static char **argv_; + static int argc_; std::string name_; SyscallInterface& syscall_; int repeat_; diff --git a/include/ditto/multithreading_utils.h b/include/ditto/multithreading_utils.h index f775e1b..b1ca472 100644 --- a/include/ditto/multithreading_utils.h +++ b/include/ditto/multithreading_utils.h @@ -87,4 +87,6 @@ struct MultithreadingParams { : name_(name), sched_attr_(sched_attr), sched_affinity_(sched_affinity) {} }; +void setproctitle(int argc, char** argv, const char* title); + } // namespace dittosuite diff --git a/src/instruction.cpp b/src/instruction.cpp index 3ea0025..41e0964 100644 --- a/src/instruction.cpp +++ b/src/instruction.cpp @@ -20,6 +20,7 @@ namespace dittosuite { + Instruction::Instruction(const std::string& name, const Params& params) : name_(name), syscall_(params.syscall_), @@ -98,10 +99,20 @@ void Instruction::SetAbsolutePathKey(int absolute_path_key) { absolute_path_key_ = absolute_path_key; } +void Instruction::SetArgv(char** argv) { + argv_ = argv; +} + +void Instruction::SetArgc(int argc) { + argc_ = argc; +} + std::string Instruction::GetAbsolutePath() { return std::get<std::string>(SharedVariables::Get(absolute_path_key_)); } int Instruction::absolute_path_key_; +char** Instruction::argv_; +int Instruction::argc_; } // namespace dittosuite diff --git a/src/multiprocessing.cpp b/src/multiprocessing.cpp index 9b9cf7f..0b37b7a 100644 --- a/src/multiprocessing.cpp +++ b/src/multiprocessing.cpp @@ -18,6 +18,7 @@ #include <sys/mman.h> #include <sys/prctl.h> +#include <sys/types.h> #include <unistd.h> namespace dittosuite { @@ -95,11 +96,10 @@ void Multiprocessing::SetUpSingle() { if (!is_manager_) { pthread_mutex_lock(initialization_mutex_); - LOGD("Trying to set the name for: " + std::to_string(instruction_id_)); - if (prctl(PR_SET_NAME, - static_cast<const char*>(thread_params_[instruction_id_].name_.c_str())) < 0) { - PLOGF("Unable to set process name"); - } + LOGD("Trying to set the name for instruction: " + std::to_string(instruction_id_) + + "; process: " + std::to_string(getpid()) + + "; new name: " + thread_params_[instruction_id_].name_); + setproctitle(argc_, argv_, (thread_params_[instruction_id_].name_.c_str())); if (thread_params_[instruction_id_].sched_attr_.IsSet()) { thread_params_[instruction_id_].sched_attr_.Set(); diff --git a/src/multithreading_utils.cpp b/src/multithreading_utils.cpp index 4d2e488..9b9716f 100644 --- a/src/multithreading_utils.cpp +++ b/src/multithreading_utils.cpp @@ -15,6 +15,7 @@ #include <ditto/multithreading_utils.h> #include <sched.h> +#include <unistd.h> namespace dittosuite { @@ -138,4 +139,97 @@ SchedAffinity& SchedAffinity::operator=(const uint64_t mask) { return *this; } +/* + * Create a copy of environ and return the new argv[0] size + * + * The stack looks pretty much as follows: + * + * | [...] + * | + * | argc + * | argv[0] (pointer) + * | argv[1] (pointer) + * | ... + * | NULL + * | environ[0] (pointer) + * | environ[1] (pointer) + * | ... + * | NULL + * | + * | [...] + * | + * | *argv[0] (string) + * | *argv[1] (string) + * | ... + * | *environ[0] (string) + * | *environ[1] (string) + * | ... + * + * After this function is call, all the *environ[*] strings will be moved in + * dynamic memory, and the old environ strings space in the stack can be used + * to extend *argv[*]. + */ +int move_environ(int argc, char**argv) { + // Count the number of items in environ + int env_last_id = -1; + if (environ) { + while (environ[++env_last_id]) + ; + } + + unsigned int argv_strings_size; + if (env_last_id > 0) { + // If there is something in environ (it exists and there is something more + // than the terminating NULL), size is the total size that will be usable + // for argv after environ is moved. In fact, this would be the size of all + // the argv strings, plus the size of all the current environ strings. + // More specifically, this is: + // - the address of the last element of environ, + // - plus its content size, so now we have the very last byte of environ, + // - subtracted the address of the first string of argv. + argv_strings_size = environ[env_last_id - 1] + strlen(environ[env_last_id - 1]) - argv[0]; + } else { + // Otherwise, this is just the size of all the argv strings. + argv_strings_size = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; + } + + if (environ) { + // Create a copy of environ in dynamic memory + char** new_environ = static_cast<char**>(malloc(env_last_id * sizeof(char*))); + + // Create a copy in dynamic memory for all the environ strings + unsigned int i = -1; + while (environ[++i]) { + new_environ[i] = strdup(environ[i]); + } + + // Also, update the environ pointer + environ = new_environ; + } + + return argv_strings_size; +} + +/* + * Update the title of the calling process by updating argv[0] (may be + * destructive for the following argv[1..]) + * + * If the length of the new title is larger than the previously allocated + * argv[0] in the stack, then this function will overwrite the next argv + * strings. + * If the length of the new title is larger than all the argv strings, this + * function will move environ and use its reclaimed space. + */ +void setproctitle(int argc, char** argv, const char* title) { + size_t available_size = strlen(argv[0]); + size_t argv_size = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; + + if (available_size < argv_size) { + available_size = move_environ(argc, argv); + } + + memset(argv[0], 0, available_size); + snprintf(argv[0], available_size - 1, "%s", title); +} + } // namespace dittosuite |