2010-03-22

【系統】Profiling PHP with Xdebug & KCachegrind

0 comments

有時候,網頁都可以正常執行,但就是慢的想讓人吐血。

這時候就可以用 Xdebug 配合 KCachegrind 來看看,到底是慢在那裡。

首先,要裝好 Xdebug ,並且在 php.ini 中,設定啟用。

1. 安裝 Xdebug

# tar zxvf xdebug-2.0.2.gz
# cd xdebug-2.0.2
# ./configure --enable-xdebug --with-php-config=/home/php/bin/php-config
# make
# make install

2. 在 Ubuntu 下安裝 KCachegrind

# sudo apt-get install KCachegrind

3. 在 php.ini 中設定啟用

[xdebug]
zend_extension="/home/php/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"
xdebug.profiler_enable=off
xdebug.profiler_enable=on
xdebug.trace_output_dir="/tmp/xdebug"
xdebug.profiler_output_dir="/tmp/xdebug"

4. 接著只要執行網頁就會在 /tmp/xdebug 下產生 cachegrind.out.xxxxx

注意:要測試時,要請其它人都不要執行,某則很難知道那個cachegrind.out 檔是你要的。

5. 接著把 cachegrind.out 檔抓回來,使用 KCachegrind 來查看結果。

載入 cachegrind.out.xxxxx

就可以看到如下圖般詳細的資訊,由其是 Call Graph 的資訊,真是超詳細的。

PS. 記得上線後要把 php.ini 裡的 Xdebug Mark 起來,不然上線會變很慢。

以上,簡單記錄一下。

2010-02-11

【系統】Ubuntu : 使用 UUID 在 fstab 中掛載磁區

0 comments

因為看到 Ubuntu 中,是使用 UUID 來掛載磁區的,

所以也就查了一下怎麼用 UUID 來掛載 NTFS 的磁區。

這樣就不用每次開機,還要再去掛載了。

首先可以先將所有硬碟 Mount 起來,然後使用 df 來看一下各磁區的資訊。

使用 blkid -s UUID,就可以看到各磁區的 UUID 資訊。

接著編輯 /etc/fstab 將UUID的資訊,配合掛載點,填入 /etc/fstab 中。就搞定了,之後重開機,就會自動掛載這三個磁區了。

上面的各欄資訊如下:
[Device] [Mount Point] [File_system] [Options] [dump] [fsck order]

其它參考網頁請見:

使用 UUID 管理檔案系統

第八章、Linux 磁碟與檔案系統管理

 

2010-02-02

【程式】ZF : Cron Tasks in Zend Framework

0 comments

最近將原本的程式全都用 Zend Framework 來改寫,

前端的部份已經完成了70%了吧!

先來看一下之後要跑的一些 Cron ,要怎麼用 Zend Framework 來執行。

看到一篇文章說的非常非常的清楚,而且一下子就搞定了。

Howto: Zend Framework Cron

我這裡來簡單記錄一下。

cron.php

define(“_CRONJOB_”,true);
require('/home/www/web/public/index.php’);

//... rest of your code goes here, you can use all Zend components now!

然後在 index.php 最後面加上

//.........略
$application->bootstrap();

/** Cronjobs don’t need all the extra’s **/
if(!defined('_CRONJOB_') || _CRONJOB_ == false) {
    $application->bootstrap()->run();
}

這樣就搞定了。

現在可以把你的程式加到 Cron 中執行了~~


另外在這裡,再補充一下,你也可以直接從 application.ini 裡設定的 DB 資訊,來建DB connection

程式片段如下:

define("_CRONJOB_",true);
require('/home/www/web/public/index.php’);
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap('db');
$dbAdapter = $bootstrap->getResource('db');
$dbAdapter->getConnection();
$stmt = $dbAdapter->select()->from('TABLE')->where("ID='119'")->query();
$result = $stmt->fetchAll();

2010-01-22

【程式】ZF : Zend_Db_Table_Select - Sub Query

0 comments

一直不想寫很複雜的SQL,但最終還是會遇到~~唉~~

這次的需求就是要從 A Table 中取得 ID。

再從 B Table 中,依 A.ID 與 B.AID 做對應,查出資料。

這部份就用到 Sub Query

附上我的程式範列:

# 程式在 A Model 
# 先從 A Model 查出需要的 ID
$tbSelect1 = $this->getTable()->select()
        ->from($this->getTable(),
            array('COUNT(*) AS CNT', 'AID'))
        ->order('CNT DESC')
        ->group('AID')
        ;

# 再跟 B Table 做 Join
$tbSelect2 = $this->getTable()->select()
        ->setIntegrityCheck(false)
        ->from(array('T' => $tbSelect1),
            array('T.AID')
            )
        ->where("T.CNT > 10")
        ->joinLeft('B',
            'T.AID = B.AID',
            array('B.TITLE', 'B.NAME'))
        ;

$res = $this->getTable()->fetchAll($tbSelect2)->toArray();

上面這程式最後產生出來的 SQL 為

SELECT "T"."AID", "B"."TITLE", "B"."NAME" FROM (
        SELECT COUNT(*) AS "CNT", "A"."MEDIA_ID"
            FROM "A" GROUP BY "AID" ORDER BY "CNT" DESC
    ) "T"
LEFT JOIN "B" ON T.ID = B.AID

 

然後還有另一種方式,是使用 IN 的方式。

# 程式在 A Model 
# 先從 A Model 查出需要的 ID
$tbSelect1 = $this->getTable()->select()
        ->from($this->getTable(),
            array( 'AID'))
        ->group('AID')
        ;

# 利用 IN 取得所需資料
$tbSelect2 = $this->getTable()->select()
        ->setIntegrityCheck(false)
        ->from('B'
            ,array('B.TITLE', 'B.NAME')
            )
        ->where(new Zend_Db_Expr("B.AID IN (". $tbSelect1." )"))
        ;

上面這段程式產生的 SQL 為

SELECT "B"."TITLE", "B"."NAME"
        FROM "B"
        WHERE (B.AID IN (
                SELECT "A"."MEDIA_ID" FROM "A" GROUP BY "AID"
        ))

就效能來看,目前好像是 第一種 Left Join 比較好。

但因為資料量少,也還沒辦法確定。待查~~

2010-01-17

【軟體】Ubuntu : XBMC - 中文無法顯示 !!

0 comments

一開始灌好 XBMC 後,發現中文都變成方格了……

解決方式,就是將裡面的字型,換成有支援繁體中文的字型。

首先將你準備好有支援繁體中文的字型 Copy 到 /usr/share/xbmc/media/Fonts 下

接著將 /usr/share/xbmc/media/Fonts下的 arial.ttf 備份一下以防萬一,

然後將你剛 Copy 到 /usr/share/xbmc/media/Fonts下的字型,更名為 arial.ttf

現在事前的準備就搞定了。

接著開啟 XBMC > System

接著選擇 Apperance

SKIN > Fonts ,會看到 Fonts 設定為 Default

現在將 Skin > Fonts > Default 改成 Arial

改好後,再看,中文已經可以正常的顯示了喔。耶~~

搞定~

如果要將整個介面都換成中文的話。

就在 International > Language ,會看到設定為 English (US) ,如下圖:

將 English 改成 Chinese ( Traditional )

就全都變成中文了。看起來還不錯吧。

 

中文設定完成。

【軟體】Ubuntu : XBMC - Media Center

0 comments

灌了 XBMC 這套軟體後,就感覺多了一個家庭劇的感覺。

網站:XBMC

如果是 Ubuntu 8.04 ,可以參考 :
Installing Xbox Media Center (XBMC) On Ubuntu 8.04

目前 Ubuntu 9.10 要用另一種外的方式來安裝。可參考:
HOW-TO install XBMC for Linux on Ubuntu, a Step-by-Step Guide

Installing XBMC Ubuntu 9.10 Karmic or higher

If you are using Ubuntu 9.10 or higher, you have the option of a more streamlined install. Load the terminal window and issue the following (The $ is a standard substitute for your terminal prompt):

     $ sudo add-apt-repository ppa:team-xbmc
     $ sudo apt-get update
     $ sudo apt-get install xbmc

安裝完後,預設的啟動就是全螢幕。下面加上一些截圖:







畫面看起來真不錯,買一台EEE PC,放在客廳,接著52吋大螢幕,這樣就超棒了啦!!

上次我看到我朋友使用 Wii 的搖桿來操作 XBMC,真方便。

非常棒的一套軟體,還可以換 SKIN ,這套軟體還支援眾多的OS,所以有興趣的可以裝來玩玩。

 

操作方式:

參考資料:

 

 

2010-01-12

【系統】Ubuntu : Custom Application Launcher

0 comments

有些程式因為不是用 Deb 安裝的,都在要 Terminal 下指令才可以執行,

還真麻煩。這時候就可以將要執行的指令,利用 Add to Panel 裡的

Custom Application Launcher 來放到上方的工具列中 ( 就很像 Windows 中下面的那條 )。

現在就來個 Sample ,我要將 Oracle SQL Developer 加到上方的工具列中。

首先在工具列上按右鍵,在選單中選擇 Add to Panel

然後會出現下面的畫面,選擇 Custom Application Launcher

在 Name 輸入要提示的 Title ,然後 Command 就是你在 Terminal 中下的指令了。

如果想改變圖示,就按上圖中左邊的 Icon,就會出現下圖讓你選擇圖示

選擇好後,圖示就變了。

然後你就會看到工具列中,最右邊會出現我們剛增加的 Oracle SQL Developer 的 Launch Icon。

點了就可以執行了。

 

以上~Over~

【軟體】Ubuntu : Oracle SQL Developer

0 comments

環境 :

  • Ubuntu 9.04
  • sun-java6-jre

前陣子 子翔 改用了 Oracle SQL Developer 來連 Oracle 資料庫,

畫面看起來不錯,我之前是使用 【軟體】DbVisualizer : 可以管理各種資料庫的好工具

但他不能直接修改資料,我覺的挺麻煩的 ( 應該是我不會用 )

所以今天就來裝了一下 Oracle SQL Developer。

首先到 Oracle 的網站下載,要下載前要註冊成會員,超麻煩的。

我直接下載 Oracle SQL Developer for 32-bit WIndows ( This zip does not include a JDK )

如下圖:

為什麼是下載 Windows 的哩,因為解開後,你可以看到下圖中,

他一樣有 sqldeveloper.sh 這個檔案。

所以就直接打開 Terminal 執行 sh sqldeveloper.sh。

我安裝的是 sun-java6-jre,他執行的時候說找不到 /bin/java,然後叫我指定路徑給他,

所以我查了一下 java 在那裡 ( whereis java ),發現是在 /usr/bin/java,所以我就輸入 /usr 後按 Enter。

PS.上面這個畫面當初沒抓到~~就一直沒抓到了

接著他說 Error: Java home /usr/bin/java is not a J2SE SDK…… Dammit!!

可以他下面又說我可以在 /path-to-sqldeveloper/sqldeveloper/bin/sqldeveloper.conf

中加上 SetSkipJ2SDKCheck true 來跳過檢查。

所以就來改一下吧 。 vi ......../bin/sqldeveloper.conf

將 SetSkipJ2SDKCheck true 加在最下面後,存檔離開。

再次執行 sh sqldeveloper.sh,終於~~出來了~~耶~~

建立資料庫連線畫面

連線成功

某資料表的 Data 畫面

 

才剛灌好,所以也還沒深入研究,不過感覺應該是不錯啦。

PS.

可以再參考 【系統】Ubuntu : Custom Application Launcher

將執行 Oracle SQL Developer 的指令直接加到上方工具列中,

這樣就可以很簡單的啟動 Oracle SQL Developer了。

以上~~

2010-01-11

【系統】Linux : BIG-5 與 UTF-8 檔案轉換

0 comments

因為Big-5的檔案,看都會有亂碼,

所以就可以用iconv來將Big-5格式的檔案,

直接轉成Utf-8的格式。

Big-5 To Utf-8

  • iconv -f big5 -t utf-8 big5.txt -o utf8.txt

Utf-8 To Big-5

  • iconv -f utf-8 -t big5 utf8.txt -o big5.txt

【系統】PHP - Fatal error: Allowed memory size of xxx bytes exhausted

0 comments

上禮拜程式執行時,居然出現這樣的錯誤訊息。

字面上看起來應該就是 Memory 的問題了。

所以解決的方法就是到 php.ini 將 memory_limit 加大。

# vi /php-etc-path/php.ini

將 memory_limit 改成

memory_limit = 128M

但數字是看各程式的需求。

另外也可以直接在程式中設定,但記得要放在整個程式的前面。

ini_set("memory_limit","128M");
?>

PS. 另外還有其他的設定
  ini_set("max_execution_time",300000);
  ini_set("max_input_time",600000);

補充說明 memory_limit : ( PHP Configuration )


memory_limit

The Memory section of the Official Requirements part of this document provides some sample values. After you consult the memory table you may decide on 12M as the correct value. You would then edit your php.ini with this value.

memory_limit = 12M

Trial and error may be required to find the right value depending on the limitations of your phpWebSite installation environment. As noted in the memory table the number of modules that you Boost with the core phpWebSite system will greatly affect how this parameter should be set. If the value in the memory_limit parameter is set too low, then you will receive fatal runtime error messages

Fatal error: Allowed memory size of m bytes exhausted (tried to allocate e bytes) in Unknown on line n

* where m is the the memory limit the was exhausted.
* where e is the number of bytes that the memory limit was exceed by.
* where n is the line number in the phpWebSite system code where the php memory limit setting was exhausted.

以上~