contributed by<williamchangTW
>
Class_Project
, Jserv
zhanyangch
tina0405
gedit /proc/sys/kernel/perf_event_paranoid
打開後想直接更換為 -1 ,可是出現 you do not have the permissions necessary to save the file.Please...
這一段使我不能更改這個檔案,我試過 sudo su
及 vim
和 gksudo
等方法去試著更改其中的值,最後我使用 sudo sh -c 'echo -1 > /proc/sys/kernel/perf_event_paranoid' ,最後是因為覺得跟 kernel 檔案的關係比較大才找到解決辦法。cat /proc/sys/kernel/perf_event_paranoid
的值確定為 -1 。再來當我輸入 perf top
如下顯示:
再來是一開始的 make plot
:(opt code copy from orig)
main.c define:
#ifdef OPT
#define OUT_FILE "opt.txt"
#else
#define OUT_FILE "orig.txt"
#endif
這裡是一種類似於 if … else 判斷式的定義,依照 #ifdef(+條件定義) 及 #else(+條件定義) #endif結束判斷式。
再來看 phonebook_opt.c code,我想從 orig去改 code 所以複製了一份去試試, trace 完後發現一個地方會忘了把 malloc free 。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "phonebook_orig.h"
/* original version */
entry *findName(char lastName[], entry *pHead)
{
while (pHead != NULL) {
if (strcasecmp(lastName, pHead->lastName) == 0)
return pHead;
pHead = pHead->pNext;
}
return NULL;
}
entry *append(char lastName[], entry *e)
{
/* allocate memory for the new entry and put lastName */
e->pNext = (entry *) malloc(sizeof(entry));
e = e->pNext;
strcpy(e->lastName, lastName);
e->pNext = NULL;
return e;
}
entry *append(char lastName[], entry *e)
{
e->pNext = (entry *) malloc(sizeof(entry));
e = e->pNext;
strcpy(e->lastName, lastName);
e->pNext = NULL;
free(e->pNext);
return e;
}
再來我朝 findName go down 開始,一開始想說在 while loop 中每次都要做判斷式,想減少他的 branch times ,因為 branch 是影響效能比重最重的一個 stage,可是想不出怎麼提出到外面解決,後來想說減少單次判斷所需等待時間著手。
先看 OPT 這個 headerfile ,於是找出 phonebook_opt.h
#ifndef _PHONEBOOK_H
#define _PHONEBOOK_H
#define MAX_LAST_NAME_SIZE 16
/* TODO: After modifying the original version, uncomment the following
* line to set OPT properly */
// #define OPT 1
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
struct __PHONE_BOOK_ENTRY *pNext;
} entry;
entry *findName(char lastName[], entry *pHead);
entry *append(char lastName[], entry *e);
#endif
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
struct __PHONE_BOOK_DET *pDet;
struct __PHONE_BOOK_ENTRY *pNext;
}entry;
typedef struct __PHONE_BOOK_DET {
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
} det;
很明顯的 find() 時間變短了,而 append() 小幅下降。
把註解掉的 OPT 打開。
#define OPT 1