Skip to content

Commit

Permalink
LcoreMgr: add clean_pid_auto_check and clean_lcore support
Browse files Browse the repository at this point in the history
./build/app/LcoreMgr --clean_pid_auto_check
./build/app/LcoreMgr --clean_lcore 30

Signed-off-by: Frank Du <[email protected]>
  • Loading branch information
frankdjx committed Nov 13, 2023
1 parent f462f54 commit a718d2d
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 37 deletions.
86 changes: 83 additions & 3 deletions app/tools/lcore_shmem_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,94 @@
* Copyright(c) 2023 Intel Corporation
*/

#include <errno.h>
#include <getopt.h>
#include <mtl/mtl_lcore_shm_api.h>
#include <stdlib.h>

#include "log.h"

enum lsm_args_cmd {
LSM_ARG_UNKNOWN = 0,
LSM_ARG_HELP = 0x100, /* start from end of ascii */
LSM_ARG_INFO,
LSM_ARG_CLEAN_PID_AUTO_CHECK,
LSM_ARG_CLEAN_LCORE,
LSM_ARG_MAX,
};

/*
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
*/
static struct option lsm_args_options[] = {
{"help", no_argument, 0, LSM_ARG_HELP},
{"info", no_argument, 0, LSM_ARG_INFO},
{"clean_pid_auto_check", no_argument, 0, LSM_ARG_CLEAN_PID_AUTO_CHECK},
{"clean_lcore", required_argument, 0, LSM_ARG_CLEAN_LCORE},
{0, 0, 0, 0},
};

static void lsm_print_help() {
printf("\n");
printf("##### Usage: #####\n\n");

printf("Params:\n");
printf(" --help: Print the help information\n");
printf(" --info: Print lcore shared manager detail info\n");
printf(" --info: Print lcore shared manager detail info\n");
printf(" --clean_pid_auto_check: Clean the dead entries if PID is not active\n");
printf(" --clean_lcore <lcore id>: Clean the entry by lcore ID\n");

printf("\n");
}

int main(int argc, char** argv) {
MTL_MAY_UNUSED(argc);
MTL_MAY_UNUSED(argv);
int cmd = -1, opt_idx = 0;
int ret;

while (1) {
cmd = getopt_long_only(argc, argv, "hv", lsm_args_options, &opt_idx);
if (cmd == -1) break;

switch (cmd) {
case LSM_ARG_INFO:
mtl_lcore_shm_print();
break;
case LSM_ARG_CLEAN_PID_AUTO_CHECK:
ret = mtl_lcore_shm_clean(MTL_LCORE_CLEAN_PID_AUTO_CHECK, NULL, 0);
if (ret > 0)
info("Total %d dead lcores detected and deleted\n", ret);
else if (ret == 0)
info("No dead lcores detected\n");
else
err("Fail %d to clean shm by auto PID check\n", ret);
break;
case LSM_ARG_CLEAN_LCORE:
int lcore = atoi(optarg);
if (lcore < 0) {
err("lcore %d is not valid\n", lcore);
return -EIO;
}
struct mtl_lcore_clean_pid_info pid;
pid.lcore = lcore;
ret = mtl_lcore_shm_clean(MTL_LCORE_CLEAN_LCORE, &pid, sizeof(pid));
if (ret >= 0)
info("Succ to delete lcore %d\n", lcore);
else
err("Fail %d to delete lcore %d\n", ret, lcore);
break;
break;
case LSM_ARG_HELP:
default:
lsm_print_help();
return -1;
}
}

mtl_lcore_shm_print();
return 0;
}
31 changes: 31 additions & 0 deletions include/mtl_lcore_shm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
extern "C" {
#endif

struct mtl_lcore_clean_pid_info {
uint32_t lcore;
};

/** lcore clean action */
enum mtl_lcore_clean_action {
/** auto, no args.
* Remove lcore usage if the PID is inactive under the same hostname and user.
*/
MTL_LCORE_CLEAN_PID_AUTO_CHECK = 0,
/** clean as PID info, args to struct mtl_lcore_clean_pid_info */
MTL_LCORE_CLEAN_LCORE,
/** max value of this enum */
MTL_LCORE_CLEAN_MAX,
};

/**
* Print out the legacy lcore manager(shared memory) status.
*
Expand All @@ -27,6 +43,21 @@ extern "C" {
*/
int mtl_lcore_shm_print(void);

/**
* Clean the unused lcore from the legacy lcore manager(shared memory).
* @param action
* The action type.
* @param args
* The args to the action type.
* @param args_sz
* The size of the args.
*
* @return
* - 0 if successful.
* - <0: Error code if fail.
*/
int mtl_lcore_shm_clean(enum mtl_lcore_clean_action action, void* args, size_t args_sz);

#if defined(__cplusplus)
}
#endif
Expand Down
31 changes: 1 addition & 30 deletions lib/src/mt_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#include "mt_main.h"

#ifndef WINDOWSENV
#include <pwd.h>
#endif

#include "datapath/mt_queue.h"
#include "dev/mt_dev.h"
#include "mt_admin.h"
Expand Down Expand Up @@ -402,31 +398,6 @@ static int _mt_stop(struct mtl_main_impl* impl) {
return 0;
}

static int mt_user_info_init(struct mtl_main_impl* impl) {
int ret = -EIO;
struct mt_user_info* info = &impl->u_info;

#ifdef WINDOWSENV /* todo */
MTL_MAY_UNUSED(ret);
snprintf(info->hostname, sizeof(info->hostname), "%s", "unknow");
snprintf(info->user, sizeof(info->user), "%s", "unknow");
#else
ret = gethostname(info->hostname, sizeof(info->hostname));
if (ret < 0) {
warn("%s, gethostname fail %d\n", __func__, ret);
snprintf(info->hostname, sizeof(info->hostname), "%s", "unknow");
}
uid_t uid = getuid();
struct passwd* user_info = getpwuid(uid);
snprintf(info->user, sizeof(info->user), "%s",
user_info ? user_info->pw_name : "unknow");
#endif

info->pid = getpid();

return 0;
}

mtl_handle mtl_init(struct mtl_init_params* p) {
struct mtl_main_impl* impl = NULL;
int socket[MTL_PORT_MAX], ret;
Expand Down Expand Up @@ -493,7 +464,7 @@ mtl_handle mtl_init(struct mtl_init_params* p) {
impl = mt_rte_zmalloc_socket(sizeof(*impl), socket[MTL_PORT_P]);
if (!impl) goto err_exit;

mt_user_info_init(impl);
mt_user_info_init(&impl->u_info);

#ifndef WINDOWSENV
if (geteuid() == 0)
Expand Down
94 changes: 90 additions & 4 deletions lib/src/mt_sch.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

#include "mt_sch.h"

#include <signal.h>

#include "mt_log.h"
#include "mt_stat.h"
#include "mtl_lcore_shm_api.h"
#include "st2110/st_rx_ancillary_session.h"
#include "st2110/st_rx_audio_session.h"
#include "st2110/st_rx_video_session.h"
Expand Down Expand Up @@ -1038,12 +1041,95 @@ int mtl_lcore_shm_print(void) {

for (int i = 0; i < RTE_MAX_LCORE; i++) {
shm_entry = &lcore_shm->lcores_info[i];
if (shm_entry->active) {
info("%s, lcore %d active on %s@%s with pid: %d\n", __func__, i, shm_entry->user,
shm_entry->hostname, (int)shm_entry->pid);
}

if (!shm_entry->active) continue;
info("%s, lcore %d active on %s@%s with pid: %d\n", __func__, i, shm_entry->user,
shm_entry->hostname, (int)shm_entry->pid);
}

sch_lcore_shm_uinit(&lcore_mgr);
return 0;
}

static int lcore_shm_clean_auto_pid(struct mt_lcore_mgr* lcore_mgr) {
struct mt_user_info u_info;
int clean = 0;

memset(&u_info, 0, sizeof(u_info));
mt_user_info_init(&u_info);

struct mt_lcore_shm* lcore_shm = lcore_mgr->lcore_shm;
struct mt_lcore_shm_entry* shm_entry;
for (int i = 0; i < RTE_MAX_LCORE; i++) {
shm_entry = &lcore_shm->lcores_info[i];

if (!shm_entry->active) continue;
if (0 != strncmp(shm_entry->hostname, u_info.hostname, sizeof(shm_entry->hostname)))
continue;
if (0 != strncmp(shm_entry->user, u_info.user, sizeof(shm_entry->user))) continue;
/* now check if PID is active with zero signal */
int result = kill(shm_entry->pid, 0);
if (0 == result) continue;
clean++;
notice("%s, delete dead lcore %d from the shared mem, PID %d\n", __func__, i,
(int)shm_entry->pid);
}

return clean;
}

static int lcore_shm_clean_id(struct mt_lcore_mgr* lcore_mgr, void* args,
size_t args_sz) {
struct mtl_lcore_clean_pid_info* info = args;

if (!args) {
err("%s, NULL args\n", __func__);
return -EINVAL;
}
if (args_sz != sizeof(*info)) {
err("%s, error args_sz %" PRIu64 "\n", __func__, args_sz);
return -EINVAL;
}
uint32_t lcore = info->lcore;
if (lcore >= RTE_MAX_LCORE) {
err("%s, invalid lcore %u\n", __func__, lcore);
return -EINVAL;
}

struct mt_lcore_shm* lcore_shm = lcore_mgr->lcore_shm;
struct mt_lcore_shm_entry* shm_entry = &lcore_shm->lcores_info[lcore];
if (!shm_entry->active) {
err("%s, lcore %u is inactive\n", __func__, lcore);
return -EINVAL;
}

shm_entry->active = false;
notice("%s, delete lcore %u from the shared mem, PID %d\n", __func__, lcore,
(int)shm_entry->pid);
return 0;
}

int mtl_lcore_shm_clean(enum mtl_lcore_clean_action action, void* args, size_t args_sz) {
struct mt_lcore_mgr lcore_mgr;

int ret = sch_lcore_shm_init(&lcore_mgr, false);
if (ret < 0) return ret;

MTL_MAY_UNUSED(args);
MTL_MAY_UNUSED(args_sz);
switch (action) {
case MTL_LCORE_CLEAN_PID_AUTO_CHECK:
ret = lcore_shm_clean_auto_pid(&lcore_mgr);
break;
case MTL_LCORE_CLEAN_LCORE:
ret = lcore_shm_clean_id(&lcore_mgr, args, args_sz);
break;
default:
err("%s, unknown action %d\n", __func__, action);
ret = -EINVAL;
break;
}

sch_lcore_shm_uinit(&lcore_mgr);
return ret;
}
28 changes: 28 additions & 0 deletions lib/src/mt_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "mt_util.h"

#ifndef WINDOWSENV
#include <pwd.h>
#endif

#include "datapath/mt_queue.h"
#include "mt_log.h"
#include "mt_main.h"
Expand Down Expand Up @@ -879,3 +883,27 @@ const char* mt_native_afxdp_port2if(const char* port) {
}
return port + strlen(native_afxdp_port_prefix);
}

int mt_user_info_init(struct mt_user_info* info) {
int ret = -EIO;

#ifdef WINDOWSENV /* todo */
MTL_MAY_UNUSED(ret);
snprintf(info->hostname, sizeof(info->hostname), "%s", "unknow");
snprintf(info->user, sizeof(info->user), "%s", "unknow");
#else
ret = gethostname(info->hostname, sizeof(info->hostname));
if (ret < 0) {
warn("%s, gethostname fail %d\n", __func__, ret);
snprintf(info->hostname, sizeof(info->hostname), "%s", "unknow");
}
uid_t uid = getuid();
struct passwd* user_info = getpwuid(uid);
snprintf(info->user, sizeof(info->user), "%s",
user_info ? user_info->pw_name : "unknow");
#endif

info->pid = getpid();

return 0;
}
2 changes: 2 additions & 0 deletions lib/src/mt_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,6 @@ const char* mt_dpdk_afpkt_port2if(const char* port);
const char* mt_kernel_port2if(const char* port);
const char* mt_native_afxdp_port2if(const char* port);

int mt_user_info_init(struct mt_user_info* info);

#endif

0 comments on commit a718d2d

Please sign in to comment.