From 720232befe69f9e1524a2507e51ed07855d571b3 Mon Sep 17 00:00:00 2001 From: Bin Qian Date: Thu, 21 Feb 2019 14:43:32 -0500 Subject: [PATCH] Enable configurable sm process priority through sm-configure In some cases sm will need to adjust its process priority. This change enables the configuring sm priority as part of sm at runtime Partial-Bug: 1816764 Change-Id: I860759621c0d1389ca5a3c947d7973c185274bdd Signed-off-by: Bin Qian --- .../sm-tools/sm_tools/sm_configure.py | 19 +++++++- service-mgmt/sm-1.0.0/src/sm_process.c | 47 +++++++++++++++---- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py b/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py index 3b89c410..60cac245 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py @@ -68,6 +68,11 @@ def main(): help='port mtce receives sm commands from' ) + sys_parser.add_argument( + "--sm_process_priority", + help='sm process nice value, range from -2 to -20, default -2.' + ) + sg_parser = subparsers.add_parser('service_group', help='Service Group ' 'Configuration') @@ -94,6 +99,15 @@ def main(): if args.sm_client_port: configure_system_opt("sm_client_port", args.sm_client_port) + + if args.sm_process_priority: + if int(args.sm_process_priority) in range(-20, -1): + configure_system_opt("sm_process_priority", + args.sm_process_priority) + else: + print("Invalid sm_process_priority value. " + "Must be between -2 to -20") + sys.exit(-1) else: database = sqlite3.connect(database_name) _dispatch_config_action(args, database) @@ -243,8 +257,9 @@ def configure_system_opt(key, value): database = sqlite3.connect(database_name) cursor = database.cursor() - sql = "UPDATE CONFIGURATION SET VALUE='%s' " \ - "WHERE KEY = '%s'" % (value, key) + sql = 'INSERT OR REPLACE INTO CONFIGURATION ( ID, "KEY", "VALUE" ) ' \ + 'VALUES((SELECT ID FROM CONFIGURATION WHERE KEY = "%s"), ' \ + '"%s", "%s");' % (key, key, value) cursor.execute(sql) database.commit() diff --git a/service-mgmt/sm-1.0.0/src/sm_process.c b/service-mgmt/sm-1.0.0/src/sm_process.c index 9a2e81ad..035069b9 100644 --- a/service-mgmt/sm-1.0.0/src/sm_process.c +++ b/service-mgmt/sm-1.0.0/src/sm_process.c @@ -53,6 +53,7 @@ #include "sm_failover.h" #include "sm_task_affining_thread.h" #include "sm_worker_thread.h" +#include "sm_configuration_table.h" #define SM_PROCESS_DB_CHECKPOINT_INTERVAL_IN_MS 30000 #define SM_PROCESS_TICK_INTERVAL_IN_MS 200 @@ -569,6 +570,34 @@ static SmErrorT sm_process_wait_node_configuration( void ) } // **************************************************************************** +// **************************************************************************** +// Process - get process priority setting +// ============== +static int get_process_nice_val() +{ + char buf[SM_CONFIGURATION_VALUE_MAX_CHAR + 1]; + int nice_val = -2; + if( SM_OKAY == sm_configuration_table_get("sm_process_priority", buf, sizeof(buf) - 1) ) + { + if(buf[0] != '\0') + { + nice_val = atoi(buf); + } + } + + if(nice_val > -2 || nice_val < -20) + { + DPRINTFE("Invalid sm_process_priority value %d, reset to default (-2)", nice_val); + nice_val = -2; + } + else + { + DPRINTFI("sm_process_priority value is set to %d", nice_val); + } + return nice_val; +} +// **************************************************************************** + // **************************************************************************** // Process - Main // ============== @@ -607,14 +636,6 @@ SmErrorT sm_process_main( int argc, char *argv[], char *envp[] ) return( SM_FAILED ); } - result = setpriority( PRIO_PROCESS, getpid(), -2 ); - if( 0 > result ) - { - DPRINTFE( "Failed to set priority of process, error=%s.", - strerror( errno ) ); - return( SM_FAILED ); - } - if( 0 > mkdir( SM_RUN_DIRECTORY, 0700 ) ) { if( EEXIST == errno ) @@ -710,6 +731,16 @@ SmErrorT sm_process_main( int argc, char *argv[], char *envp[] ) return( error ); } + int process_nice_val = get_process_nice_val(); + + result = setpriority( PRIO_PROCESS, getpid(), process_nice_val); + if( 0 > result ) + { + DPRINTFE( "Failed to set priority of process, error=%s.", + strerror( errno ) ); + return( SM_FAILED ); + } + error = sm_utils_set_boot_complete(); if( SM_OKAY != error ) {