2016年12月12日 星期一

[linux] 量測特定 process CPU ussage





Preparation

To calculate CPU usage for a specific process you'll need the following:
  1. /proc/uptime
    • #1 uptime of the system (seconds)
  2. /proc/[PID]/stat
    • #14 utime - CPU time spent in user code, measured in clock ticks
    • #15 stime - CPU time spent in kernel code, measured in clock ticks
    • #16 cutime - Waited-for children's CPU time spent in user code (in clock ticks)
    • #17 cstime - Waited-for children's CPU time spent in kernel code (in clock ticks)
    • #22 starttime - Time when the process started, measured in clock ticks
  3. Hertz (number of clock ticks per second) of your system.




Calculation

First we determine the total time spent for the process:
total_time = utime + stime
We also have to decide whether we want to include the time from children processes. If we do, then we add those values to total_time:
total_time = total_time + cutime + cstime
Next we get the total elapsed time in seconds since the process started:
seconds = uptime - (starttime / Hertz)
Finally we calculate the CPU usage percentage:
cpu_usage = 100 * ((total_time / Hertz) / seconds)





此例來說 bash 的 CPU usage 為
1.160880760010654


2016年12月11日 星期日

[Android] 改設定以免 Android Studio 吃 C槽 系統硬碟空間


1.
C:\Program Files\Android Studio\bin\idea.propertis

idea.config.path
idea.system.path

2.
環境變數
Variable name: ANDROID_SDK_HOME
Variable value: D:\Android



3.


gradle-setting.jpg


參考這篇但改變順序
以免重工
http://www.littlecpu.com/android-studio-c-drive




2016年12月6日 星期二

[C]如何在 linux 和 Mac build C share library

以此例來說,quanto2 是我們要build 的目的檔案



gcc -fPIC -g -c quanto2.c -o libquanto2.o

Linux:
gcc -g -shared -Wl,-soname,libquanto2.so -o libquanto2.so libquanto2.o -lc
MacOS:
gcc -g -shared -Wl,-install_name,libquanto2.so -o libquanto2.so libquanto2.o -lc


gcc -g test.c -o test -L. -lquanto2

[Go] 如何和 C 語言做介接

以這個為例 ,quanto2 是我的自定義 C library
這樣就可以call 到 C 的 function



// #cgo LDFLAGS: -L ./ -lquanto2
// #include <stdio.h>
// #include <stdlib.h>
// #include "quanto2.h"
import "C"
import "fmt"

func main() {
    fmt.Println("ho ho")
    C.testGo()
}

2016年11月24日 星期四

[Android][AWS] How to iterate all AWS S3 bucket access right 如何列舉 AWS S3 的所有 bucket

             




                AWSCredentials creden = new BasicAWSCredentials("AKIAIIZCDEMXYJP5G3RA", "oBy8PZKgxAMKxw9sEhmvjfZ0seHNvE2SBLgyWN8i");
                AmazonS3Client s3Client = new AmazonS3Client(creden);
                List<Bucket> buckets = s3Client.listBuckets();
                for (Bucket bucket : buckets) {
//                    Log.d("charles", bucket.getName());
                    try {
                        ListObjectsRequest lor = new ListObjectsRequest()
                                .withBucketName(bucket.getName());
                        ObjectListing objectListing = s3Client.listObjects(lor);
                        for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
//                            Log.d("charles", summary.getKey());
                        }
                        Log.d("charles", bucket.getName() + " :  granted");
                    } catch (AmazonS3Exception e) {
                        Log.d("charles", bucket.getName() + " :  AccessDenied");
                    }
                }

[Android][AWS] How to iterate all AWS S3 bucket access right 如何列舉 AWS S3 的所有 bucket

             

                AWSCredentials creden = new BasicAWSCredentials("AKIAIIZCDEMXYJP5G3RA", "oBy8PZKgxAMKxw9sEhmvjfZ0seHNvE2SBLgyWN8i");
                AmazonS3Client s3Client = new AmazonS3Client(creden);
                List<Bucket> buckets = s3Client.listBuckets();
                for (Bucket bucket : buckets) {
//                    Log.d("charles", bucket.getName());
                    try {
                        ListObjectsRequest lor = new ListObjectsRequest()
                                .withBucketName(bucket.getName());
                        ObjectListing objectListing = s3Client.listObjects(lor);
                        for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
//                            Log.d("charles", summary.getKey());
                        }
                        Log.d("charles", bucket.getName() + " :  granted");
                    } catch (AmazonS3Exception e) {
                        Log.d("charles", bucket.getName() + " :  AccessDenied");
                    }
                }

2016年11月22日 星期二

[Android][AWS] How to connent to AWS S3 get data


get Keys form AWS S3

AmazonS3 s3 = new AmazonS3Client(CRED);
ListObjectsRequest lor = new ListObjectsRequest()
                             .withBucketName("myBucket")
                             .withPrefix("code/");
ObjectListing objectListing = s3.listObjects(lor);
for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
    System.out.println(summary.getKey());
}


 AWSCredentials creden=new BasicAWSCredentials(accessKey,secretKey);
    AmazonS3Client s3Client=new AmazonS3Client(creden);
S3Object object = s3Client.getObject(new GetObjectRequest(
                "BucketName", "keyName"));

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                object.getObjectContent()));
        Writer writer = new OutputStreamWriter(new FileOutputStream(file));

        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;
            writer.write(line + "\n");
        }
    writer.flush();
    writer.close();
    reader.close();



http://stackoverflow.com/questions/17561432/amazon-s3-listing-directories

2016年10月17日 星期一

How to Modify the Shell Path in macOS

1. cd  cd~
2. vi .bash_profile
3. export PATH="/Users/charles.lo/Library/Android/sdk/platform-tools:$PATH"

MAC finder show hide hidden file

顯示隱藏檔案:defaults write com.apple.finder AppleShowAllFiles TRUE;\killall Finder

不顯示隱藏檔案:defaults write com.apple.finder AppleShowAllFiles FALSE;\killall Finder

Install Geany on Mac OSX

About the App

Install the App

  1. Press Command+Space and type Terminal and press enter/return key.
  2. Run in Terminal app:
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null
    and press enter/return key. Wait for the command to finish.
  3. Run:
    brew cask install geany

2016年10月14日 星期五

Git tab autocomplete on OSX 10.11 El Capitan



Step 1: Download auto completion script:


>> cd ~ 
>> curl -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash


Step 2: Update .bash_profile


>> echo "source ~/git-completion.bash" >> .bash_profile


2016年10月13日 星期四

How to Enable Tab Completion in Mac OS X Terminal



  • Use pico, nano, vi or whatever your favorite text editor is to edit/create .inputrc, I’ll use pico for this walkthrough.
pico .inputrc
  • Paste in the following three lines of rules:
set completion-ignore-case on
set show-all-if-ambiguous on
TAB: menu-complete
  • Press Control+O to save changes to .inputrc followed by Control+X to quit
  • Close and reopen Terminal for the rules to take effect
  • Start typing a path or command and press the Tab key to see this feature in action

2016年10月2日 星期日

Beautiful Binary String

Alice has a binary string, of length . She thinks a binary string is beautiful if and only if it doesn't contain thesubstring .
In one step, Alice can change a  to a  (or vice-versa). Count and print the minimum number of steps needed to make Alice see the string as beautiful.
Input Format
The first line contains an integer,  (the length of binary string ).
The second line contains a single binary string, , of length .
Constraints
  • Each character in .
Output Format
Print the minimum number of steps needed to make the string beautiful.
Sample Input 0
7
0101010
Sample Output 0
2
Sample Input 1
5
01100
Sample Output 1
0
Sample Input 2
10
0100101010
Sample Output 2
3
Explanation
Sample Case 0:
In this sample, 
The figure below shows a way to get rid of each instance of :
Because we were able to make the string beautiful by changing  characters ( and ), we print .
Sample Case 1:
In this sample 
The substring  does not occur in , so the string is already beautiful and we print .

====================================================
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String B = in.next();
        int i = 0, out = 0, next = 0;
        if (B.indexOf("010") >= 0) {
            while (i < B.length()) {
                next = B.indexOf("010", i) + 3;
                if (next < i) {
                    break;
                }
                i = next;    
                out++;
            }
        } else {}
        
        System.out.println(out);
    }
}