Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Thursday, January 12, 2023

[Linux] 使用 bash shell 監測某個 daemon 的執行緒數量

 #!/bin/sh

# Define the process name

process_name=rsyslogd


# Get minimum number of threads from command line parameters

process_min_threads=$1


# Calculate number of threads for the given process name

num_threads=`ps -e -T -f | grep “${process_name}“ | grep -v “grep” | wc -l`


# Restart process when thread count is less than expected

if [ ${num_threads} -lt ${process_min_threads} ]; then


    echo “Need to restart ${process_name} since current thread num ${num_threads} is less than ${process_min_threads}“


fi


From:

https://ephrain.net/linux-%E4%BD%BF%E7%94%A8-bash-shell-%E7%9B%A3%E6%B8%AC%E6%9F%90%E5%80%8B-daemon-%E7%9A%84%E5%9F%B7%E8%A1%8C%E7%B7%92%E6%95%B8%E9%87%8F/

[Linux] 最大執行緒數限制及當前執行緒數查詢

 1、總結系統限制有:

/proc/sys/kernel/pid_max #查系統支援的最大執行緒數,一般會很大,相當於理論值

/proc/sys/kernel/thread-max

max_user_process(ulimit -u) #系統限制某使用者下最多可以執行多少程序或執行緒

/proc/sys/vm/max_map_count

硬體記憶體大小



2、Java虛擬機器本身限制:

-Xms #intial java heap size

-Xmx #maximum java heap size

-Xss #the stack size for each thread



3、查詢當前某程式的執行緒或程序數

pstree -p `ps -e | grep java | awk '{print $1}'` | wc -l

pstree -p 3660 | wc -l



4、查詢當前整個系統已用的執行緒或程序數

pstree -p | wc -l


1、 cat /proc/${pid}/status


2、pstree -p ${pid}


3、top -p ${pid} 再按H 或者直接輸入 top -bH -d 3 -p ${pid}


top -H

手冊中說:-H : Threads toggle

加上這個選項啟動top,top一行顯示一個執行緒。否則,它一行顯示一個程序。


4、ps xH

手冊中說:H Show threads as if they were processes

這樣可以檢視所有存在的執行緒。


5、ps -mp <PID>

手冊中說:m Show threads after processes

這樣可以檢視一個程序起的執行緒數。


使用 bash shell 監測某個 daemon 的執行緒數量

[Linux] cpu 相關 command

 判斷依據:

1.具有相同core id的cpu是同一個core的超執行緒。

2.具有相同physical id的cpu是同一顆cpu封裝的執行緒或者cores。


#邏輯CPU個數

cat /proc/cpuinfo | grep "processor" | wc -l

 

echo "physical CPU number:"

#物理CPU個數:

cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l

 

echo "core number in a physical CPU:"

#每個物理CPU中Core的個數:

cat /proc/cpuinfo | grep "cpu cores" | uniq | awk -F: '{print $2}'

 

#查看每個physical cpu上core id的數量,即為每個物理CPU上的core的個數

cat /proc/cpuinfo | grep "core id"

 

#是否為超執行緒?

#如果有兩個邏輯CPU core具有相同的”core id”,那麼超執行緒是打開的。

# 當然也可知直接查詢 /proc/cpuinfo 中的 "ht"這個flag

cat /proc/cpuinfo | grep flags | grep ht

 

#每個物理CPU中邏輯CPU(可能是core, threads或both)的個數:

cat /proc/cpuinfo | grep "siblings"

Linux Index

Computer Science from the Bottom Up

CPU上下文切換的次數和時間 (context switch)

Linux-Command Online Explain

  查 Wi-Fi  dongle chip

解析 Linux 共享記憶體機制

Shell Script 截取部份字串

What does the dot at the end of the permissions in the output of "ls -lah" mean?

ubuntu/debian 大部分都不啟用 selinux ,所以都沒有;但 redhat/centos 預設有啟用 selinux,就會有

sed

vim

Makefile

cpu 相關 command

最大執行緒數限制及當前執行緒數查詢

  

Monday, October 25, 2021

[Linux] PS

[root@study ~]# ps aux  <==觀察系統所有的程序資料

[root@study ~]# ps -lA  <==也是能夠觀察所有系統的資料

[root@study ~]# ps axjf <==連同部分程序樹狀態


while true; do
ps -eo pid,ppid,cmd,%mem,%cpu --sort -pcpu,+pmem
ps -eo pid,ppid,%mem,%cpu --sort -pcpu,+pmem | awk '{if ($3!=0){total1 += $3;print "MEM:"$3"%";}} END { print "MEM:"total1"%" }'
ps -eo pid,ppid,%mem,%cpu --sort -pcpu,+pmem | awk '{if ($4!=0){total2 += $4;print "CPU:"$4"%";}} END { print "CPU:"total2"%" }'

sleep 3;
done&

當要刪除 while.... 

先查詢 pid
 PID TTY TIME CMD
15312 pts/84 00:00:00 ps
18095 pts/84 00:00:00 bash

 


kill -9 pid

Reference:

https://linux.cn/article-4743-1.html

https://blog.longwin.com.tw/2017/04/linux-ps-process-show-cpu-memory-2017/


Saturday, February 13, 2021

Makefile -example

ARM_CC = arm-linux-gnueabi-gcc
CC = gcc
CXX = g++
LDFLAGS = -lm
CFLAGS = -Wall -pedantic -g -std=c99 -lpthread 

OBJS = main.o                       \
       GyroscopeBiasEstimator.o     \
       HeadTracker.o                \
       HeadTransform.o              \
       LowPassFilter.o              \
       Matrix3x3d.o                 \
       Matrix.o                     \
       OrientationEKF.o             \
       So3Util.o                    \
       Vector3d.o                   \
       MadgwickAHRS.o

main: ${OBJS}
    ${CC} -o main ${OBJS} ${LDFLAGS} ${CFLAGS}
clean:
    rm -f main ${OBJS}

編譯多個執行檔

ARM_CC = arm-linux-gnueabi-gcc
CC = gcc
CXX = g++
LDFLAGS = -lm
CFLAGS = -Wall -pedantic -ggdb3 -O0

0505_OBJS = 0505.o
2DMI_OBJS = 2DMI.o
IFBP2D_OBJS = IFBP2D.o
IFBP_OBJS = IFBP.o

all: 0505 2DMI IFBP2D IFBP

0505: ${0505_OBJS}
    ${CXX} -o $@ $? 

2DMI: ${2DMI_OBJS}
    ${CXX} -o $@ $?

IFBP2D: ${IFBP2D_OBJS}
    ${CXX} -o $@ $?

IFBP: ${IFBP2D_OBJS}
    ${CXX} -o $@ $?

.SUFFIXES : .cpp
.cpp.o:
    $(CXX) $(CFLAGS) $(LDFLAGS) -c $<

clean:
    rm -f *.o 0505 2DMI IFBP2D IFBP

編譯兩個so檔案最後編譯成執行檔

add

int add(int a, int b)
{
    return a + b;
}

sub

int sub(int a, int b) 
{
    return a - b;
}

main.c

#include <stdio.h>

int a = 100;
int b = 200;

int add(int,int);
int sub(int,int);

int main(int argc, char* argv[])
{
    printf("add=%d\n", add(a, b));
    printf("sub=%d\n", sub(a, b));
    return 0;
}

Makefile

all: add.so sub.so main 有順序問題要先編譯出so檔案在main , 所以main擺最後

CC = gcc
SOFLAGS = -shared -fPIC
LDFLAGS = -lm
CFLAGS = -Wall -pedantic -ggdb3 -O0 -std=c99 -lpthread

all: add.so sub.so main

main:
    ${CC}  -o main  main.c -L./ -ladd -L./ -lsub ${CFLAGS} ${LDFLAGS} 

add.so: add.c
    $(CC) ${SOFLAGS} -o libadd.so $? 

sub.so: sub.c
    $(CC) ${SOFLAGS} -o libsub.so $? 

.PHONY: clean
clean:
    rm -f main *.so *.o



 

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能