0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SMPでのthreadマイグレーション

Last updated at Posted at 2025-02-25

MT7621にはせっかく4つもthreadがあるので、ちゃんと動いてるか確かめたくなりました。

FreeBSDのコマンドセットではxzがマルチスレッドに対応しているようです。

試したところTオプションをつけてもまったく変わりません。どうも小さいデータではマイグレーションしないみたいです。読み書きできるストレージがmdの10Mしかないので、大きなデータは試せません。

スケジューラーはULEを使っていますが、マイグレーションはkern.schedのパラメータでコストを忖度して行われるようです。

ネットで拾ったコードをいじってテストプログラムを作ってみました。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int done;

int fibonacci (int x)
{
    if (x <= 1) {
        return 1;
    }
    return fibonacci(x-1) + fibonacci(x-2);
}

void *runner(void *param)
{
        fibonacci((int)param);
        ++done;
        pthread_exit(0);
}

int main(int argc, char *argv[])
{
        int count, i;
        pthread_attr_t attr; 
        int num = 42;

        if (argc != 2) {
                fprintf(stderr,"usage: fibonacci <integer value>\n");
                return -1;
        }

        count = atoi(argv[1]);

        if (count < 1) {
                fprintf(stderr,"%d must be>= 1\n", count);
                return -1;
        }

        done = 0;

        pthread_attr_init(&attr);

        for(i = 1;i <= count; ++i){
                pthread_t thread;
                pthread_create(&thread,&attr,runner,(void*)num);
                pthread_detach(thread); 
        }

        while (done != count)
                sleep(1);
}

結果

# time ./fibonacci 1
       43.73 real        43.35 user         0.00 sys
# time ./fibonacci 2
       50.13 real        99.74 user         0.00 sys
# time ./fibonacci 4
       67.71 real       265.68 user         0.01 sys

ちゃんと動いてますね。ただふくすうにしたときのオーバヘッドが大きいです。

おうちのルーターなどでこれほど負荷がかかることはなくkern.schedの設定は調整が必要と思われます。

LUNA88Kでも似たような事をしていた記憶があります。当時はMACHのcthreadを使っていたような気がします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?