GadgetSeed  0.9.6
com_task.c
[詳解]
1 /** @file
2  @brief タスク関連コマンド
3 
4  @date 2012.01.22
5  @author Takashi SHUDO
6 
7  @section task_command taskコマンド
8 
9  task コマンドには以下のサブコマンドがあります。
10 
11  | サブコマンド | 機能 | 詳細 |
12  |:------------------|:------------------------------|:----------------------|
13  | top | @copybrief com_task_top | @ref com_task_top |
14  | list | @copybrief com_task_list | @ref com_task_list |
15  | queue | @copybrief com_task_queue | @ref com_task_queue |
16  | kill | @copybrief com_task_kill | @ref com_task_kill |
17  | calltrace | @copybrief com_task_calltrace | @ref com_task_calltrace |
18 */
19 
20 #include "sysconfig.h"
21 #include "shell.h"
22 #include "str.h"
23 #include "tkprintf.h"
24 #include "tprintf.h"
25 #include "task/task.h"
26 #include "task/syscall.h"
27 #include "task/calltrace.h"
28 
29 //#define DEBUGTBITS 0x02
30 #include "dtprintf.h"
31 
32 
33 #ifndef GSC_KERNEL_MESSAGEOUT_MEMORY_SIZE
34 #define GSC_KERNEL_MESSAGEOUT_MEMORY_SIZE (1024 * 3) ///< $gsc カーネルメッセージ出力メモリサイズ
35 #endif
36 static unsigned char kmess_buff[GSC_KERNEL_MESSAGEOUT_MEMORY_SIZE];
37 
38 #ifndef GSC_MAX_TASK_INFO_NUM
39 #define GSC_MAX_TASK_INFO_NUM 10 ///< $gsc topコマンドで表示可能な最大タスク数
40 #endif
41 
42 static struct st_task_info task_info_list[GSC_MAX_TASK_INFO_NUM];
43 static unsigned int last_run_time[GSC_MAX_TASK_INFO_NUM];
44 
45 static int task_top(int argc, uchar *argv[])
46 {
47  int i, count;
48  int flg_scroll = 0;
49  int flg_start = 0;
50  struct st_task_info *ti;
51  unsigned long sum_time;
52 
53  if(argv[1][0] == 's') {
54  flg_scroll = 1;
55  }
56 
57  count = task_get_tasks_info(task_info_list, GSC_MAX_TASK_INFO_NUM);
58 
59  do {
60  ti = task_info_list;
61  for(i=0; i<count; i++) {
62  last_run_time[i] = ti->run_time;
63  ti ++;
64  }
65 
66  if(cwait(1000) != 0) {
67  unsigned char rd;
68  if(cgetcnw(&rd) != 0) {
69  return 0;
70  }
71  }
72 
73  count = task_get_tasks_info(task_info_list, GSC_MAX_TASK_INFO_NUM);
74 
75  sum_time = 0;
76  ti = task_info_list;
77  for(i=0; i<count; i++) {
78  sum_time += (ti->run_time - last_run_time[i]);
79  ti ++;
80  }
81 
82  ti = task_info_list;
83  if(flg_scroll == 0) {
84  if(flg_start == 0) {
85  tprintf("\033[2J"); // 画面クリア
86  flg_start = 1;
87  }
88  tprintf("\033[0;0H"); // カーソルを0,0へ
89  }
90  tprintf("PID TASK-NAME PRI RUN-TIME(us) %%CPU\n");
91  for(i=0; i<count; i++) {
92  unsigned long run_time = ti->run_time - last_run_time[i];
93  unsigned long long percent = 0;
94  if(sum_time != 0) {
95  percent = ((unsigned long long)run_time * 10000) / sum_time;
96  }
97  tprintf("%3d %15s %3d %10ld %3ld.%02ld\n",
98  ti->id, ti->name, ti->priority, run_time,
99  (unsigned long)percent/100, (unsigned long)percent%100);
100  ti ++;
101  }
102  //tprintf("%10ld\n", sum_time); // DEBUG
103  } while(1);
104 
105  return 0;
106 }
107 
108 /**
109  @brief 実行中タスクのCPU使用率を表示する
110 */
111 static const struct st_shell_command com_task_top = {
112  .name = "top",
113  .command = task_top,
114  .usage_str = "top [\"s\"(scroll)]",
115  .manual_str = "Print task load"
116 };
117 
118 
119 static int task_list(int argc, uchar *argv[])
120 {
121  unsigned int mess_len;
122 
124  print_task_list();
125  mess_len = set_kernel_message_out_mem(0, 0);
126  cputs(kmess_buff, mess_len);
127 
128  return 0;
129 }
130 
131 /**
132  @brief 実行中タスクの状態を表示する
133 */
134 static const struct st_shell_command com_task_list = {
135  .name = "list",
136  .command = task_list,
137  .manual_str = "Print task list"
138 };
139 
140 
141 static int task_queue(int argc, uchar *argv[])
142 {
143  unsigned int mess_len;
144 
146  print_task_queue();
147  mess_len = set_kernel_message_out_mem(0, 0);
148  cputs(kmess_buff, mess_len);
149 
150  return 0;
151 }
152 
153 /**
154  @brief 各タスクキューに登録されているタスクを表示する
155 */
156 static const struct st_shell_command com_task_queue = {
157  .name = "queue",
158  .command = task_queue,
159  .manual_str = "Print task queue"
160 };
161 
162 
163 static int command_task_kill(int argc, uchar *argv[]);
164 
165 /**
166  @brief 任意のタスクを終了させる
167 */
168 static const struct st_shell_command com_task_kill = {
169  .name = "kill",
170  .command = command_task_kill,
171  .usage_str = "<PID>",
172  .manual_str = "Kill task"
173 };
174 
175 static int command_task_kill(int argc, uchar *argv[])
176 {
177  if(argc < 2) {
178  print_command_usage(&com_task_kill);
179  return 0;
180  }
181 
182  task_kill(dstoi(argv[1]));
183 
184  return 0;
185 }
186 
187 
188 
189 #ifdef GSC_KERNEL_ENABLE_CALLTRACE
190 static int command_call_trace(int argc, uchar *argv[])
191 {
192  unsigned int mess_len;
193 
195  print_call_trace();
196  mess_len = set_kernel_message_out_mem(0, 0);
197  cputs(kmess_buff, mess_len);
198 
199  return 0;
200 }
201 
202 /**
203  @brief タスクAPIの実行ログを表示する
204 
205  このコマンドを有効にするには GSC_KERNEL_ENABLE_CALLTRACE をコンフィグレーションで定義する必要があります
206 */
207 static const struct st_shell_command com_task_calltrace = {
208  .name = "calltrace",
209  .command = command_call_trace,
210  .manual_str = "Print calltrace"
211 };
212 #endif
213 
214 
215 static const struct st_shell_command * const com_task_com_list[] = {
216  &com_task_top,
217  &com_task_list,
219  &com_task_kill,
220 #ifdef GSC_KERNEL_ENABLE_CALLTRACE
221  &com_task_calltrace,
222 #endif
223  0
224 };
225 
226 const struct st_shell_command com_task = {
227  .name = "task",
228  .manual_str = "Task operation commands",
229  .sublist = com_task_com_list
230 }; ///< タスク状態取得
unsigned char uchar
GadgetSeedの文字(列)は unsigned char 型となる
Definition: str.h:13
シェルコマンド構造体
Definition: shell.h:33
タスク情報
Definition: task.h:36
コマンドシェル
static const struct st_shell_command com_task_queue
各タスクキューに登録されているタスクを表示する
Definition: com_task.c:156
int cputs(unsigned char *str, unsigned int count)
標準出力より文字列を出力する
Definition: console.c:137
void task_kill(int id)
指定したタスクを終了する
Definition: syscall_api.c:304
アプリケーション(タスク)デバッグ用マクロ
static const struct st_shell_command com_task_list
実行中タスクの状態を表示する
Definition: com_task.c:134
int priority
タスクプライオリティ
Definition: task.h:39
int cgetcnw(unsigned char *rd)
標準入力より1文字を取得する(待ち無し)
Definition: console.c:235
文字列処理
カーネル用機能限定printf
int dstoi(uchar *str)
10進数文字列 int 変換
Definition: str.c:523
uchar name[12]
コマンド名文字列
Definition: shell.h:34
static const struct st_shell_command com_task_kill
任意のタスクを終了させる
Definition: com_task.c:168
int tprintf(const char *fmt,...)
簡易printf
Definition: tprintf.c:85
システムコール
int id
タスクID
Definition: task.h:37
unsigned int set_kernel_message_out_mem(unsigned char *mp, unsigned int size)
カーネルメッセージ出力メモリアドレスを設定する
Definition: tkprintf.c:79
デバッグ用システムコールトレース
#define GSC_MAX_TASK_INFO_NUM
$gsc topコマンドで表示可能な最大タスク数
Definition: com_task.c:39
タスク制御
static const struct st_shell_command com_task_top
実行中タスクのCPU使用率を表示する
Definition: com_task.c:111
char name[TASK_NAME_LEN+1]
タスク名文字列
Definition: task.h:38
int cwait(unsigned int timeout)
標準入力より入力を待つ
Definition: console.c:219
機能限定printf
unsigned int run_time
タスク実行時間
Definition: task.h:41
#define GSC_KERNEL_MESSAGEOUT_MEMORY_SIZE
$gsc カーネルメッセージ出力メモリサイズ
Definition: com_task.c:34