Add --set-priority option in dotnettool
authorj-h.choi <j-h.choi@samsung.com>
Tue, 2 Jan 2024 07:52:01 +0000 (16:52 +0900)
committer조웅석/MDE Lab(SR)/삼성전자 <ws77.cho@samsung.com>
Mon, 5 Feb 2024 04:04:05 +0000 (13:04 +0900)
Change-Id: I9d7c0291e24e6be28e6974be360e5c271f9c3c1e

NativeLauncher/inc/ni_common.h
NativeLauncher/tool/dotnettool.cc
NativeLauncher/tool/ni_common.cc

index c4e6a8f..0493216 100644 (file)
@@ -40,6 +40,7 @@
 #define NI_FLAGS_PRINT_CMD              0x0200
 #define NI_FLAGS_SKIP_RO_APP            0x0400
 #define NI_FLAGS_RM_ORIGIN_AFTER_NI     0x0800
+#define NI_FLAGS_SET_PRIORITY           0x1000
 
 typedef std::function<void (std::string)> afterCreate;
 
@@ -61,6 +62,7 @@ typedef struct NIOption{
        std::vector<std::string> extraRefPath;
        std::vector<std::string> inputBubbleRefFiles;
        std::vector<std::string> mibcPath;
+       int priority;
 } NIOption;
 
 /**
@@ -146,4 +148,9 @@ ni_error_e regenerateAppNI(NIOption* opt);
  */
 ni_error_e regenerateTACNI(NIOption* opt);
 
+/**
+ * @brief Sets the priority of the process to the specified values from -20 to 19.(default : 0)
+ */
+void setPriority(NIOption* opt);
+
 #endif /* __NI_COMMON_H__ */
index 4343b49..628124c 100644 (file)
@@ -75,7 +75,9 @@ void DisplayUsage() {
                "                                   (This option works with --ni-regen-all-app only)\n"
                "       --rm-origin-after-ni      - Remove original dll after creating native image\n"
                "                                   Note!: App ATOC options(--ni-pkg, --ni-regen-all-app, --tac-regen-all) cannot be used with --rm-origin-after-ni option.\n"
-               "                                   (Use only for assemblies that will not be AOTed again afterward.)"
+               "                                   (Use only for assemblies that will not be AOTed again afterward.)\n"
+               "       --set-priority            - Sets the priority of the process to the specified values from -20(highest priority) to 19(lowest priority).(default : 0)\n"
+
                "\n"
                "Usage: dotnettool [options] [command] [arguments]\n"
                "\n"
@@ -136,6 +138,23 @@ int main(int argc, char* argv[])
                        opt->flags |= NI_FLAGS_SKIP_RO_APP;
                }  else if (arg == "--rm-origin-after-ni") {
                        opt->flags |= NI_FLAGS_RM_ORIGIN_AFTER_NI;
+               } else if (arg == "--set-priority") {
+                       ++i;
+                       if (i >= argc) {
+                               _SOUT("Priority value(-20 ~ 19) should be followed for --set-priority option");
+                               DisplayUsage();
+                               return 0;
+                       }
+
+                       opt->flags |= NI_FLAGS_SET_PRIORITY;
+                       try {
+                               opt->priority = std::stoi(argv[i]);
+                               setPriority(opt);
+                       } catch (...) {
+                               _SERR("Invalid argument: %s", argv[i]);
+                               DisplayUsage();
+                               return 0;
+                       }
                } else if (arg == "--mibc") {
                        ++i;
                        if (i >= argc) {
@@ -417,7 +436,9 @@ int main(int argc, char* argv[])
 
        gettimeofday(&tv, NULL);
        endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
-       _SOUT("\nSpend time for dotnettool is [%d]ms", (int)(endtime - starttime));
+       int ms = (int)(endtime - starttime);
+       float sec = (float)(ms/1000);
+       _SOUT("\nSpend time for dotnettool is [%d]ms, [%.3f]sec", ms, sec);
 
        return 0;
 }
index df98335..7cc1d63 100644 (file)
@@ -25,6 +25,8 @@
 #include <wait.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 
 #include <algorithm>
 #include <string>
@@ -505,6 +507,22 @@ static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::str
        return NI_ERROR_NONE;
 }
 
+void setPriority(NIOption* opt)
+{
+       pid_t pid = getpid();
+       if (setpriority(PRIO_PROCESS, pid, opt->priority) == 0) {
+               std::string str = " ";
+               if (opt->priority <= -20) {
+                       str = " highest ";
+               } else if (opt->priority >= 19) {
+                       str = " lowest ";
+               }
+               _SOUT("Success to set the%spriority of the process. pid : [%d], priority : [%d]", str.c_str(), pid, getpriority(PRIO_PROCESS, pid));
+       } else {
+               _SERR("Failed to set the priority of the process. pid : [%d], priority : [%d]", pid, getpriority(PRIO_PROCESS, pid));
+       }
+}
+
 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
 {
        // fork crossgen2
@@ -527,6 +545,9 @@ static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, con
                        return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
                }
        } else {
+               if (opt->flags & NI_FLAGS_SET_PRIORITY) {
+                       setPriority(opt);
+               }
                std::vector<const char*> argv;
                makeArgs(argv, refPaths, opt);
                argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
@@ -600,6 +621,9 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
                                return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
                        }
                } else {
+                       if (opt->flags & NI_FLAGS_SET_PRIORITY) {
+                               setPriority(opt);
+                       }
                        std::vector<const char*> argv;
                        makeArgs(argv, refPaths, opt);