Thursday, July 5, 2012

CE: Controlling Perfman

    HWND topWindow;
    TCHAR szBuf[50] = {0};
    static bool fLogIsActive = FALSE;
    TCHAR newName[255] = {0};
    FILE *tempFile;

    topWindow = GetForegroundWindow();
    if(topWindow != NULL){
        GetWindowText(topWindow, szBuf, sizeof(szBuf)-1);
        if(wcscmp(szBuf, L"CeLog/Profiler")){
            MessageBox(NULL,L"WRONG WINDOW!!!\nLaunch Perfman first and go to Celog/Profile tab",L"BattLevelPlus", 0);           
        }else{
           
            if(fLogIsActive == FALSE){
                //Check perfman log file and rename it. Then, delete it.
                tempFile = fopen("\\My Documents\\Perfman.clg","r");
                if(tempFile){
                    swprintf(newName,L"\\My Documents\\Perfman%d.clg",logCount);
                    MoveFile(gPerfmanLogDefName,newName);
                    fclose(tempFile);
                }
               
                //Wait 5 seconds
                Sleep(5000);

                mouse_event(MOUSEEVENTF_MOVE+MOUSEEVENTF_ABSOLUTE, BUTTON_X, BUTTON_Y, 0, 0);
                mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTDOWN, BUTTON_X, BUTTON_Y, 0, 0);
                mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTUP, BUTTON_X, BUTTON_Y, 0, 0);
                fLogIsActive = TRUE;
            }else{
                mouse_event(MOUSEEVENTF_MOVE+MOUSEEVENTF_ABSOLUTE, BUTTON_X, BUTTON_Y, 0, 0);
                mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTDOWN, BUTTON_X, BUTTON_Y, 0, 0);
                mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTUP, BUTTON_X, BUTTON_Y, 0, 0);
                fLogIsActive = FALSE;       
            }
        }
    }

=================================================================

What does this code do? Well this is how you can control Profiler tool for CE devices known as Perfman. This tool logs a .clg file in which after you use "Readlog" (see: http://msdn.microsoft.com/en-us/library/ms905162.aspx), you can view how the system performs over a period of time. Running Perfman for more than 10 minutes with dump a very big log file which could be very difficult to convert or open. So what you can do is to collect this log as maybe 5 minute intervals.



This function does not access any API except for mouse event because I don't know which API exposes control to Perfman. So what I did is to have Perfman on the foreground window all the time while I'm running this. It forces the mouse to run an event that emulates touching the Start/Stop button of Perfman while it sees the app as the top window. It will also rename the perfman.clg log file so that previous log files can be differentiated.


No comments: