Add --set-priority option in dotnettool
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 4796ad3..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>
@@ -251,7 +253,7 @@ static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt
        std::string prevPath;
 
        prevPath = getBaseName(absDllPath);
-       niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
+       niDirPath = concatPath(prevPath, APP_NI_SUB_TMP_DIR);
 
        if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
                niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
@@ -492,6 +494,12 @@ static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::str
                }
        }
 
+       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+               if (!removeFile(dllPath)) {
+                       _SERR("Fail to remove original file : %s", dllPath.c_str());
+               }
+       }
+
        if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
                _SOUT("Native image %s generated successfully.", outFile.c_str());
        }
@@ -499,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
@@ -521,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);
@@ -594,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);
 
@@ -647,7 +677,15 @@ static ni_error_e createCoreLibNI(NIOption* opt)
 
        if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
                if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE) {
-                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+                       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+                               std::ofstream output(coreLibBackup);
+                               if (!exist(coreLibBackup)) {
+                                       _SERR("Failed to create System.Private.CoreLib.dll.Backup");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }
+                               copySmackAndOwnership(__pm->getRuntimePath(), coreLibBackup, false);
+                               output.close();
+                       } else if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
                                return NI_ERROR_CORE_NI_FILE;
                        }
@@ -655,7 +693,7 @@ static ni_error_e createCoreLibNI(NIOption* opt)
                                if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
                                        _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
                                        return NI_ERROR_CORE_NI_FILE;
-                               }                               
+                               }
                        } else {
                                if (rename(niCoreLib.c_str(), coreLib.c_str())) {
                                        _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.dll");
@@ -670,6 +708,23 @@ static ni_error_e createCoreLibNI(NIOption* opt)
        return NI_ERROR_NONE;
 }
 
+static void renameAppNITmpPath(NIOption* opt)
+{
+       std::string niTmpPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_TMP_DIR;
+       std::string niPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_DIR;
+
+       if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
+               niTmpPath = replaceAll(niTmpPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+               niPath = replaceAll(niPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+       }
+
+       if (isDirectory(niTmpPath)) {
+               if (rename(niTmpPath.c_str(), niPath.c_str())) {
+                       _SERR("Fail to rename from .native_image_tmp to .native_image");
+               }
+       }
+}
+
 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
@@ -744,22 +799,27 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                }
        }
 
-       if (ret == NI_ERROR_NONE && opt->flags & NI_FLAGS_INPUT_BUBBLE) {
-               for (auto &dll : dllList) {
-                       std::string tmpFile;
-                       std::string niFile;
-                       if (opt->flags & NI_FLAGS_APPNI) {
-                               niFile = getAppNIFilePath(dll, opt);
-                       } else {
-                               niFile = getNIFilePath(dll);
-                       }
-                       tmpFile = niFile + ".tmp";
+       if (ret == NI_ERROR_NONE) {
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                       for (auto &dll : dllList) {
+                               std::string tmpFile;
+                               std::string niFile;
+                               if (opt->flags & NI_FLAGS_APPNI) {
+                                       niFile = getAppNIFilePath(dll, opt);
+                               } else {
+                                       niFile = getNIFilePath(dll);
+                               }
+                               tmpFile = niFile + ".tmp";
 
-                       if (exist(tmpFile)) {
-                               moveFile(tmpFile, niFile);
-                               _SOUT("Native image %s generated successfully.", niFile.c_str());
+                               if (exist(tmpFile)) {
+                                       moveFile(tmpFile, niFile);
+                                       _SOUT("Native image %s generated successfully.", niFile.c_str());
+                               }
                        }
                }
+               if (opt->flags & NI_FLAGS_APPNI) {
+                       renameAppNITmpPath(opt);
+               }
        }
 
        return ret;
@@ -1088,8 +1148,13 @@ void removeNIUnderDirs(const std::string& rootPaths)
 {
        auto convert = [](const std::string& path, const std::string& filename) {
                if (isNativeImage(path)) {
-                       if (remove(path.c_str())) {
-                               _SERR("Failed to remove %s", path.c_str());
+                       std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
+                       if (exist(assemblyPath)) {
+                               if (remove(path.c_str())) {
+                                       _SERR("Failed to remove %s", path.c_str());
+                               }
+                       } else {
+                               _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
                        }
                }
        };
@@ -1152,7 +1217,7 @@ ni_error_e regenerateAppNI(NIOption* opt)
        if (ret != PMINFO_R_OK)
                return NI_ERROR_UNKNOWN;
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE_TRUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;
@@ -1172,6 +1237,7 @@ ni_error_e regenerateAppNI(NIOption* opt)
 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
        char *pkgId = NULL;
+       char *root = NULL;
        NIOption **pOpt = (NIOption**)userData;
 
        int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
@@ -1180,6 +1246,18 @@ static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
+       ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
+       if (ret != PMINFO_R_OK) {
+               _SERR("Failed to get root path");
+               return -1;
+       }
+
+       std::string binPath = concatPath(std::string(root), "bin");
+       if (exist(concatPath(binPath, PRE_COMPILED_PACKAGE_FILE))) {
+               _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId);
+               return 0;
+       }
+
        sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
        if (!tac_db) {
                _SERR("Sqlite open error");
@@ -1221,7 +1299,7 @@ ni_error_e regenerateTACNI(NIOption* opt)
                return NI_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;