add multi-core support; move IP and MAC vars to Utilities

add more comments in SSLogger
This commit is contained in:
HappyZ 2017-01-27 14:37:45 -08:00
parent ca1cbfd736
commit 8149f6f021
9 changed files with 240 additions and 211 deletions

View File

@ -27,6 +27,7 @@ import java.io.InputStreamReader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity { public class MainActivity extends Activity {
// unchanged stuff // unchanged stuff
@ -69,8 +70,6 @@ public class MainActivity extends Activity {
protected static int currentBandwidth = -1; // bps, default is -1, indicating unlimited protected static int currentBandwidth = -1; // bps, default is -1, indicating unlimited
protected static TextView txt_results; protected static TextView txt_results;
protected static Handler myHandler; protected static Handler myHandler;
protected static String myInetIP = "";
protected static String myMAC = "";
protected static String RXportNum = "4444"; protected static String RXportNum = "4444";
protected static String outFolderPath; protected static String outFolderPath;
protected static String btn_click_time; protected static String btn_click_time;
@ -486,9 +485,11 @@ public class MainActivity extends Activity {
+ existedItems[selectedItems.get(i)] + "_" + existedItems[selectedItems.get(i)] + "_"
+ (bytes2send / 1024) + "KB_" + (bytes2send / 1024) + "KB_"
+ repeatCounts + "repeats_thrpt_" + repeatCounts + "repeats_thrpt_"
+ (currentBandwidth / 1000000.0) + "MBps_" + (currentBandwidth == -1 ? "Unlimited" :
+ new SimpleDateFormat("yyyyMMdd_HHmmss") (currentBandwidth / 1000000.0) + "MBps_")
.format(new Date()) + (new SimpleDateFormat(
"yyyyMMdd_HHmmss", Locale.US)
.format(new Date()))
+ ".tar.gz"; + ".tar.gz";
commd[2] = "cd " + outFolderPath + "/" commd[2] = "cd " + outFolderPath + "/"
+ existedItems[selectedItems.get(i)] + existedItems[selectedItems.get(i)]
@ -600,11 +601,14 @@ public class MainActivity extends Activity {
binary_RX_RawNormal = "client_recv_bypassl3"; binary_RX_RawNormal = "client_recv_bypassl3";
// get number of cores // get number of cores
coreNum = Utilities.getNumCores(); coreNum = Utilities.getNumCores();
// output folder for SSLogger // output folder for SSLogger
outFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SSLogger"; outFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SSLogger";
if (!Utilities.dirExist(outFolderPath, true)) { if (!Utilities.dirExist(outFolderPath, true)) {
// checked and cannot create this folder
Toast.makeText(this, "Cannot create folder!!!", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Cannot create folder!!!", Toast.LENGTH_LONG).show();
} }
// elements in the page // elements in the page
txt_results = (TextView) findViewById(R.id.txt_results); txt_results = (TextView) findViewById(R.id.txt_results);
btn_startTransmit = (Button) findViewById(R.id.btn_startTransmit); btn_startTransmit = (Button) findViewById(R.id.btn_startTransmit);
@ -615,12 +619,7 @@ public class MainActivity extends Activity {
btn_setOthers = (Button) findViewById(R.id.btn_setOthers); btn_setOthers = (Button) findViewById(R.id.btn_setOthers);
btn_setLogFreq = (Button) findViewById(R.id.btn_setLogFreq); btn_setLogFreq = (Button) findViewById(R.id.btn_setLogFreq);
btn_clearStatus = (Button) findViewById(R.id.btn_clearStatus); btn_clearStatus = (Button) findViewById(R.id.btn_clearStatus);
if (coreNum > 2) {
txt_results.append(
"Only support 2 cores now! Contact Yanzi to add "
+ coreNum + " cores support!\n");
}
// TODO: remember to add more than 2 core support
txt_results.append(isUsingWifi?getString(R.string.stat_wifion):getString(R.string.stat_wifioff)); txt_results.append(isUsingWifi?getString(R.string.stat_wifion):getString(R.string.stat_wifioff));
// click listener // click listener
btn_startTransmit.setOnClickListener(new View.OnClickListener() { btn_startTransmit.setOnClickListener(new View.OnClickListener() {

View File

@ -19,14 +19,16 @@ import java.util.ArrayList;
/** /**
* Created by yanzi on 9/20/15. * Created by yanzi on 9/20/15.
* two core only right now * Updated by yanzi on 01/27/2017
* support multiple cores now
*/ */
public class SSLogger extends Service { public class SSLogger extends Service {
private static final String TAG = "SSLogger"; private static final String TAG = "SSLogger";
private boolean isRunning = false; private boolean isRunning = false;
private boolean isRunningPollingThread = false; private boolean isRunningPollingThread = false;
private String ssFileName, cpuFileName, private String ssFileName, cpuFileName,
cpuWiFiDriverPIDFileName, cpuPerProcPIDFileName, cpuTCPDumpFileName, cpuAppSelfFileName; cpuWiFiDriverPIDFileName, cpuPerProcPIDFileName,
cpuTCPDumpFileName, cpuAppSelfFileName;
private int wifiRSS, gsmRSS; private int wifiRSS, gsmRSS;
private static int tcpdumpPID = -1, appSelfPID = -1; private static int tcpdumpPID = -1, appSelfPID = -1;
private WifiManager wm; private WifiManager wm;
@ -40,79 +42,113 @@ public class SSLogger extends Service {
Log.d(TAG, "already running polling thread"); Log.d(TAG, "already running polling thread");
return; return;
} }
// prevent multiple polling threads
isRunningPollingThread = true; isRunningPollingThread = true;
// variables
int i;
byte[] cpuRawStuff, ssStuff = null,
cpuWiFiDriverPIDStuff = null,
cpuPerPIDStuff = null,
cpuTCPDumpStuff = null,
cpuAppSelfStuff = null;
String mTime; // holder for system time
String tmp; // placeholder for constructed string
String[] cpuUsage = new String[MainActivity.coreNum + 1]; // placeholder for each core
String[] cpuFreq = new String[MainActivity.coreNum]; // placeholder for each core
// first line to write is instruction
tmp = "# timestamp totalUsage";
for (i = 0; i < MainActivity.coreNum; ++i) {
tmp += " cpu" + i + " freq" + i;
}
tmp += "\n";
// if SSLogger is set to run
while(isRunning) { while(isRunning) {
// get current system time // get current system time
String mTime = Long.toString(System.currentTimeMillis()); mTime = Long.toString(System.currentTimeMillis());
String[] raws = new String[5]; // assume 4 cores, [0] is total
// read current cpu usage // read current cpu usage
readUsage(raws); readUsage(cpuUsage);
String myTmp = mTime + " " + raws[0] + " ";
for (int i = 0; i < MainActivity.coreNum - 1; ++i) { // read current cpu frequency
myTmp += raws[i+1] + " " + cpuFrequency(i) + " "; readFrequency(cpuFreq);
// construct bytes for cpuRaw log
tmp += mTime + " " + cpuUsage[0];
for (i = 0; i < MainActivity.coreNum; ++i) {
tmp += " " + cpuUsage[i + 1] + " " + cpuFreq[i];
} }
myTmp += raws[MainActivity.coreNum] + " " tmp += "\n";
+ cpuFrequency(MainActivity.coreNum - 1) + "\n"; cpuRawStuff = tmp.getBytes();
// // cpuTotal
// String[] cpuTotal = raws[0].split("\\s+"); // construct bytes for PID log
// String cpuTotalsum = parseProcStat(cpuTotal); if (MainActivity.wifiDriverPID != -1) {
// // cpu 1 - N (N = coreNum) cpuWiFiDriverPIDStuff = (mTime + " "
// String[] cpu1 = raws[1].split("\\s+"); + parseProcPIDStat(readUsagePID(MainActivity.wifiDriverPID))
// String cpu1sum = parseProcStat(cpu1); + "\n").getBytes();
// String[] cpu2 = null, cpu3 = null, cpu4 = null; }
// String cpu2sum = null, cpu3sum = null, cpu4sum = null;
// // parse lines // construct bytes for per process pid (just running process)
// if (MainActivity.coreNum > 1) { if (MainActivity.isLoggingPerProcPID) {
// cpu2 = raws[2].split("\\s+"); cpuPerPIDStuff = (mTime + " "
// cpu2sum = parseProcStat(cpu2); + parseProcPIDStat(readUsagePID(MainActivity.perProcPID))
// } + "\n").getBytes();
// if (MainActivity.coreNum > 2) { }
// cpu3 = raws[3].split("\\s+");
// cpu3sum = parseProcStat(cpu3); // construct bytes for tcpdump
// } if (tcpdumpPID != -1) {
// if (MainActivity.coreNum > 3) { cpuTCPDumpStuff = (mTime + " "
// cpu4 = raws[4].split("\\s+"); + parseProcPIDStat(readUsagePID(tcpdumpPID)) + "\n").getBytes();
// cpu4sum = parseProcStat(cpu4); }
// }
byte[] cpuStuff, ssStuff = null, // construct bytes for logging app
cpuWiFiDriverPIDStuff = null, cpuPerPIDStuff = null, cpuTCPDumpStuff = null, if (MainActivity.isLoggingAppSelf) {
cpuAppSelfStuff = null; cpuAppSelfStuff = (mTime + " "
cpuStuff = myTmp.getBytes(); + parseProcPIDStat(readUsagePID(appSelfPID)) + "\n").getBytes();
if (MainActivity.wifiDriverPID != -1) }
cpuWiFiDriverPIDStuff = (mTime + " " + parseProcPIDStat(readUsagePID(MainActivity.wifiDriverPID)) + "\n").getBytes();
if (MainActivity.isLoggingPerProcPID) // construct bytes for wifi rss
cpuPerPIDStuff = (mTime + " " + parseProcPIDStat(readUsagePID(MainActivity.perProcPID)) + "\n").getBytes(); if (wifiRSS != 0) {
if (tcpdumpPID != -1)
cpuTCPDumpStuff = (mTime + " " + parseProcPIDStat(readUsagePID(tcpdumpPID)) + "\n").getBytes();
if (MainActivity.isLoggingAppSelf)
cpuAppSelfStuff = (mTime + " " + parseProcPIDStat(readUsagePID(appSelfPID)) + "\n").getBytes();
if (wifiRSS != 0)
ssStuff = (mTime + " wifi " + wifiRSS + "\n").getBytes(); ssStuff = (mTime + " wifi " + wifiRSS + "\n").getBytes();
}
// write results into file
try { try {
if (isRunning) { if (isRunning) {
if (wifiRSS != 0) { if (wifiRSS != 0) {
// Log.d(TAG, "Wrote stuff: " + ssStuff);
os_ss.write(ssStuff); os_ss.write(ssStuff);
} }
os_cpu.write(cpuStuff);
os_cpu.write(cpuRawStuff);
if (MainActivity.wifiDriverPID != -1) if (MainActivity.wifiDriverPID != -1)
os_cpuWiFiDriverPID.write(cpuWiFiDriverPIDStuff); os_cpuWiFiDriverPID.write(cpuWiFiDriverPIDStuff);
if (MainActivity.isLoggingPerProcPID) if (MainActivity.isLoggingPerProcPID)
os_cpuPerProcPID.write(cpuPerPIDStuff); os_cpuPerProcPID.write(cpuPerPIDStuff);
if (tcpdumpPID != -1) if (tcpdumpPID != -1)
os_cpuTCPDump.write(cpuTCPDumpStuff); os_cpuTCPDump.write(cpuTCPDumpStuff);
if (MainActivity.isLoggingAppSelf) if (MainActivity.isLoggingAppSelf)
os_cpuAppSelf.write(cpuAppSelfStuff); os_cpuAppSelf.write(cpuAppSelfStuff);
} }
} catch (IOException unimportant) { } catch (IOException unimportant) {
Log.w(TAG, "IO error at SSLogger"); Log.w(TAG, "IO error at SSLogger");
} }
// sleep for a while and then log to prevent high IO (and cpu)
try { try {
Thread.sleep(MainActivity.time_wait_for); Thread.sleep(MainActivity.time_wait_for);
} catch (Exception unimportant) { } catch (Exception unimportant) {
Log.w(TAG, "Can't wait.."); Log.w(TAG, "Can't wait.. something wrong?");
} }
} }
// end of polling thread
isRunningPollingThread = false; isRunningPollingThread = false;
} }
} }
@ -121,27 +157,34 @@ public class SSLogger extends Service {
* my initialization * my initialization
*/ */
public void initialization() { public void initialization() {
// no need to check the directory again since main activity already did
File mDir = new File(MainActivity.outFolderPath);
// run tcpdump // run tcpdump
if (MainActivity.isUsingTCPDump) { if (MainActivity.isUsingTCPDump) {
try { try {
Runtime.getRuntime().exec("su -c " + MainActivity.binaryFolderPath Runtime.getRuntime().exec(
"su -c " + MainActivity.binaryFolderPath
+ "tcpdump -i " + MainActivity.tcpdumpInterface + "tcpdump -i " + MainActivity.tcpdumpInterface
+ " -w " + MainActivity.outFolderPath + "/tcpdump_wifionly_" + " -w " + MainActivity.outFolderPath + "/tcpdump_wifionly_"
+ MainActivity.btn_click_time + " &" + MainActivity.btn_click_time + " &"
).waitFor(); ).waitFor();
if (MainActivity.isVerbose) {
Log.d(TAG, "TCPDump started");
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// get the tcpdump pid if requested
if (MainActivity.isLoggingTCPDump) if (MainActivity.isLoggingTCPDump)
tcpdumpPID = Utilities.getMyPID(MainActivity.binary_tcpdump, true); tcpdumpPID = Utilities.getMyPID(MainActivity.binary_tcpdump, true);
} }
// get the logging app pid if requested
if (MainActivity.isLoggingAppSelf) if (MainActivity.isLoggingAppSelf)
appSelfPID = Utilities.getMyPID("offloading", true); appSelfPID = Utilities.getMyPID("offloading", true);
// permission error
if (!Utilities.canWriteOnExternalStorage()) { if (!Utilities.canWriteOnExternalStorage()) {
MainActivity.myHandler.post(new Runnable() { MainActivity.myHandler.post(new Runnable() {
@Override @Override
@ -151,41 +194,52 @@ public class SSLogger extends Service {
}); });
onDestroy(); onDestroy();
} }
// get the initial WiFi signal strength // get the initial WiFi signal strength
wm = (WifiManager) this.getSystemService(WIFI_SERVICE); wm = (WifiManager) this.getSystemService(WIFI_SERVICE);
if (!wm.isWifiEnabled() && MainActivity.isVerbose) { if (!wm.isWifiEnabled() && MainActivity.isVerbose) {
MainActivity.myHandler.post(new Runnable() { MainActivity.myHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
MainActivity.txt_results.append("WiFi remains OFF!\n"); MainActivity.txt_results.append("WiFi should be ON! Check.\n");
} }
}); });
wifiRSS = 0; wifiRSS = 0;
} else { } else {
wifiRSS = wm.getConnectionInfo().getRssi(); wifiRSS = wm.getConnectionInfo().getRssi();
this.registerReceiver(this.myWifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION)); // register to fetch rssi upon change
this.registerReceiver(this.myWifiReceiver,
new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
} }
// create folder
File mDir = new File(MainActivity.outFolderPath); // signal strength file string
// mDir.mkdir();
if (wifiRSS != 0) { if (wifiRSS != 0) {
// Log.d(TAG, "wifi rss is not 0");
ssFileName = MainActivity.btn_click_time.concat(".ss"); ssFileName = MainActivity.btn_click_time.concat(".ss");
} }
// cpu raw usage & frequency file string
cpuFileName = MainActivity.btn_click_time.concat(".cpuRaw"); cpuFileName = MainActivity.btn_click_time.concat(".cpuRaw");
// file handler for cpu usage of wifi driver
if (MainActivity.wifiDriverPID != -1) if (MainActivity.wifiDriverPID != -1)
cpuWiFiDriverPIDFileName = MainActivity.btn_click_time.concat(".cpuPID"); cpuWiFiDriverPIDFileName = MainActivity.btn_click_time.concat(".cpuPID");
// file string for cpu usage of my process
if (MainActivity.isLoggingPerProcPID) if (MainActivity.isLoggingPerProcPID)
cpuPerProcPIDFileName = MainActivity.btn_click_time.concat(".cpuProcPID"); cpuPerProcPIDFileName = MainActivity.btn_click_time.concat(".cpuProcPID");
// file string for cpu usage of tcpdump
if (tcpdumpPID != -1) if (tcpdumpPID != -1)
cpuTCPDumpFileName = MainActivity.btn_click_time.concat(".cpuTCPDump"); cpuTCPDumpFileName = MainActivity.btn_click_time.concat(".cpuTCPDump");
// file string for cpu usage of logging app
if (MainActivity.isLoggingAppSelf) if (MainActivity.isLoggingAppSelf)
cpuAppSelfFileName = MainActivity.btn_click_time.concat(".cpuAppSelf"); cpuAppSelfFileName = MainActivity.btn_click_time.concat(".cpuAppSelf");
// create file output stream
try { try {
if (wifiRSS != 0) { if (wifiRSS != 0) {
// Log.d(TAG, "create os_ss handler"); os_ss = new FileOutputStream(new File(mDir, ssFileName));
File tmp = new File(mDir, ssFileName);
os_ss = new FileOutputStream(tmp);
} }
os_cpu = new FileOutputStream(new File(mDir, cpuFileName)); os_cpu = new FileOutputStream(new File(mDir, cpuFileName));
if (MainActivity.wifiDriverPID != -1) if (MainActivity.wifiDriverPID != -1)
@ -214,28 +268,28 @@ public class SSLogger extends Service {
@Override @Override
public void onCreate(){ public void onCreate(){
super.onCreate(); super.onCreate();
// Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
} }
@Override @Override
public void onDestroy(){ public void onDestroy(){
isRunning = false; isRunning = false;
// get rid of wifi rssi monitor
if (wifiRSS != 0) if (wifiRSS != 0)
this.unregisterReceiver(this.myWifiReceiver); this.unregisterReceiver(this.myWifiReceiver);
// kill tcpdump // kill tcpdump
if (MainActivity.isUsingTCPDump) { if (MainActivity.isUsingTCPDump) {
try { try {
Runtime.getRuntime().exec("su -c killall -9 tcpdump").waitFor(); Runtime.getRuntime().exec("su -c killall -9 tcpdump").waitFor();
if (MainActivity.isVerbose) } catch (InterruptedException | IOException e) {
Log.d(TAG, "TCPDump ended");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (MainActivity.isLoggingTCPDump) if (MainActivity.isLoggingTCPDump)
tcpdumpPID = -1; tcpdumpPID = -1;
} }
// close all file handler
try { try {
if (os_ss != null) if (os_ss != null)
os_ss.close(); os_ss.close();
@ -260,12 +314,15 @@ public class SSLogger extends Service {
return null; return null;
} }
/**
* get wifi rssi
*/
private BroadcastReceiver myWifiReceiver = new BroadcastReceiver(){ private BroadcastReceiver myWifiReceiver = new BroadcastReceiver(){
@Override @Override
public void onReceive(Context arg0, Intent arg1){ public void onReceive(Context arg0, Intent arg1){
wifiRSS = arg1.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0); wifiRSS = arg1.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0);
Log.d(TAG, "WiFi RSSI: " + wifiRSS);
if (MainActivity.isVerbose) { if (MainActivity.isVerbose) {
Log.d(TAG, "WiFi RSSI: " + wifiRSS);
MainActivity.myHandler.post(new Runnable() { MainActivity.myHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -318,21 +375,8 @@ public class SSLogger extends Service {
private static void readUsage(String[] raws) { private static void readUsage(String[] raws) {
try { try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine(); for (int i = 0; i <= MainActivity.coreNum; ++i) {
raws[0] = load; raws[i] = reader.readLine();
load = reader.readLine();
raws[1] = load;
if (MainActivity.coreNum > 1) {
load = reader.readLine();
raws[2] = load;
}
if (MainActivity.coreNum > 2) {
load = reader.readLine();
raws[3] = load;
}
if (MainActivity.coreNum > 3) {
load = reader.readLine();
raws[4] = load;
} }
reader.close(); reader.close();
} catch (IOException unimportant) { } catch (IOException unimportant) {
@ -355,25 +399,25 @@ public class SSLogger extends Service {
} }
/** /**
* get the frequency of the cpu * get the frequency of all cpu cores
* @param cpuid which cpu * @param raws
* @return long
*/ */
private static long cpuFrequency(int cpuid) { private static void readFrequency(String[] raws) {
String filepath;
RandomAccessFile reader;
try { try {
String pathFile = "/sys/devices/system/cpu/cpu" + cpuid + "/cpufreq/scaling_cur_freq"; for (int cpuid = 0; cpuid < MainActivity.coreNum; ++cpuid) {
RandomAccessFile reader = new RandomAccessFile(pathFile, "r"); filepath = "/sys/devices/system/cpu/cpu" + cpuid + "/cpufreq/scaling_cur_freq";
String load = reader.readLine(); reader = new RandomAccessFile(filepath, "r");
String[] toks = load.split("\\s+"); String[] toks = reader.readLine().split("\\s+");
long cpuScaling = Long.parseLong(toks[0]); raws[cpuid] = toks[0];
reader.close(); reader.close();
return cpuScaling; }
} catch (FileNotFoundException unimportant) {
// Log.w(TAG, "exception on cpuFreq");
return -1;
} catch (IOException unimportant) { } catch (IOException unimportant) {
// Log.w(TAG, "exception on cpuFreq"); Log.w(TAG, "exception on cpuFrequency");
return -1;
} }
} }
} }

View File

@ -9,7 +9,7 @@ import java.io.InputStreamReader;
/** /**
* Created by yanzi on 9/18/15. * Created by yanzi on 9/18/15.
* Updated on 01/25/17 * Updated on 01/27/17
*/ */
public class Thread_TX_CNormal implements Runnable { public class Thread_TX_CNormal implements Runnable {
@ -19,12 +19,16 @@ public class Thread_TX_CNormal implements Runnable {
@Override @Override
public void run() { public void run() {
// prevent multiple runs
if (MainActivity.isRunning_TX_Normal) if (MainActivity.isRunning_TX_Normal)
return; return;
if (MainActivity.isVerbose)
Log.d("TX_Normal", "Start TX Normal");
MainActivity.isRunning_TX_Normal = true; MainActivity.isRunning_TX_Normal = true;
// variables
Process proc; Process proc;
String stdout;
BufferedReader stdout_buf, error_buf;
String[] commd = new String[3]; String[] commd = new String[3];
// get the right command // get the right command
@ -35,11 +39,12 @@ public class Thread_TX_CNormal implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_Normal + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_Normal + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myInetIP : MainActivity.remoteIP) + " " + (MainActivity.isLocal ? Utilities.myInetIP : MainActivity.remoteIP) + " "
+ MainActivity.RXportNum + " " + MainActivity.RXportNum + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));
Log.d("TX_Normal", "Start TX Normal");
try { try {
// run process // run process
proc = Runtime.getRuntime().exec(commd); proc = Runtime.getRuntime().exec(commd);
@ -48,17 +53,18 @@ public class Thread_TX_CNormal implements Runnable {
while (MainActivity.isLoggingPerProcPID && MainActivity.perProcPID == -1) { while (MainActivity.isLoggingPerProcPID && MainActivity.perProcPID == -1) {
MainActivity.perProcPID = Utilities.getMyPID(MainActivity.binary_TX_Normal, false); MainActivity.perProcPID = Utilities.getMyPID(MainActivity.binary_TX_Normal, false);
} }
proc.waitFor(); proc.waitFor();
// read error // read error
BufferedReader error_buf = new BufferedReader(new InputStreamReader( error_buf = new BufferedReader(new InputStreamReader(
proc.getErrorStream())); proc.getErrorStream()));
final String error = error_buf.readLine(); // only one line error final String error = error_buf.readLine(); // only one line error
error_buf.close();
// read std out // read std out
BufferedReader stdout_buf = new BufferedReader(new InputStreamReader( stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream())); proc.getInputStream()));
String stdout;
// get sent bytes // get sent bytes
stdout = stdout_buf.readLine(); stdout = stdout_buf.readLine();
@ -92,46 +98,14 @@ public class Thread_TX_CNormal implements Runnable {
throughput = Utilities.parseBinOutput(stdout); throughput = Utilities.parseBinOutput(stdout);
} }
// InputStream stdout = proc.getInputStream(); stdout_buf.close();
// byte[] buffer = new byte[20];
// int read; } catch (IOException | InterruptedException e) {
// StringBuilder out = new StringBuilder();
// while(true){
// read = stdout.read(buffer);
// if(read<0){
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Err in TX_Normal: " + error + "\n");
// }
// });
// break;
// }
// out.append(new String(buffer, 0, read));
// if(read<20){
// break;
// }
// }
// final String mOut = out.toString().trim();
// if (!mOut.equals(""))
// MainActivity.reportedFinishTime = Double.parseDouble(mOut);
// else
// MainActivity.reportedFinishTime = 0.0;
// if (MainActivity.isVerbose) {
// MainActivity.myHandler.post(new Runnable() {
// @Override
// public void run() {
// MainActivity.txt_results.append("Time: " + mOut + "ms\n");
// }
// });
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (MainActivity.isVerbose)
Log.d("TX_Normal", "Stop TX Normal"); Log.d("TX_Normal", "Stop TX Normal");
MainActivity.isRunning_TX_Normal = false; MainActivity.isRunning_TX_Normal = false;
MainActivity.perProcPID = -1; MainActivity.perProcPID = -1;
} }

View File

@ -35,7 +35,7 @@ public class Thread_TX_CNormalUDP implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_NormalUDP + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_NormalUDP + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myInetIP : MainActivity.remoteIP) + " " + (MainActivity.isLocal ? Utilities.myInetIP : MainActivity.remoteIP) + " "
+ MainActivity.RXportNum + " " + MainActivity.RXportNum + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));

View File

@ -34,7 +34,7 @@ public class Thread_TX_CRawNormal implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_RawNormal + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_RawNormal + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myMAC : MainActivity.remoteMAC) + " " + (MainActivity.isLocal ? Utilities.myMAC : MainActivity.remoteMAC) + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));

View File

@ -35,7 +35,7 @@ public class Thread_TX_CRawSplice implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_RawSplice + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_RawSplice + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myInetIP : MainActivity.remoteIP) + " " + (MainActivity.isLocal ? Utilities.myInetIP : MainActivity.remoteIP) + " "
+ MainActivity.RXportNum + " " + MainActivity.RXportNum + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));

View File

@ -34,7 +34,7 @@ public class Thread_TX_CSendfile implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_Sendfile + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_Sendfile + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myInetIP : MainActivity.remoteIP) + " " + (MainActivity.isLocal ? Utilities.myInetIP : MainActivity.remoteIP) + " "
+ MainActivity.RXportNum + " " + MainActivity.RXportNum + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));

View File

@ -34,7 +34,7 @@ public class Thread_TX_CSplice implements Runnable {
commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"") commd[2] = (MainActivity.isForcingCPU0?"taskset 1 ":"")
+ MainActivity.binaryFolderPath + MainActivity.binary_TX_Splice + " " + MainActivity.binaryFolderPath + MainActivity.binary_TX_Splice + " "
+ MainActivity.bytes2send + " " + MainActivity.bytes2send + " "
+ (MainActivity.isLocal ? MainActivity.myInetIP : MainActivity.remoteIP) + " " + (MainActivity.isLocal ? Utilities.myInetIP : MainActivity.remoteIP) + " "
+ MainActivity.RXportNum + " " + MainActivity.RXportNum + " "
+ ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf( + ((MainActivity.currentBandwidth < 0) ? "" : String.valueOf(
MainActivity.currentBandwidth)); MainActivity.currentBandwidth));

View File

@ -27,9 +27,15 @@ import java.util.List;
/** /**
* Created by yanzi on 10/1/15. * Created by yanzi on 10/1/15.
* Updated by yanzi on 01/27/2017
*/ */
public class Utilities { public class Utilities {
private static final String TAG = "Utilities"; private static final String TAG = "Utilities";
// variables
protected static String myInetIP = "127.0.0.1";
protected static String myMAC = "";
// Storage Permissions // Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1; private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = { private static String[] PERMISSIONS_STORAGE = {
@ -75,39 +81,22 @@ public class Utilities {
* get the ip and mac addresses * get the ip and mac addresses
*/ */
protected static void getSelfIdentity(String interface_name, boolean useIPv4) { protected static void getSelfIdentity(String interface_name, boolean useIPv4) {
String name;
Enumeration<NetworkInterface> networks;
Enumeration<InetAddress> inetAddresses;
NetworkInterface network;
try { try {
Enumeration<NetworkInterface> networks = networks = NetworkInterface.getNetworkInterfaces();
NetworkInterface.getNetworkInterfaces();
while (networks.hasMoreElements()) { while (networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement(); network = networks.nextElement();
// check if the interface matches the desired one // check if the interface matches the desired one
String name = network.getDisplayName(); name = network.getDisplayName();
if (!name.equals(interface_name)) if (!name.equals(interface_name))
continue; continue;
Log.d(TAG, "myInterface: " + interface_name);
// get the ip address
Enumeration<InetAddress> inetAddresses = network.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String sAddr = inetAddress.getHostAddress();
boolean isIPv4 = sAddr.indexOf(':') < 0;
// check if we only want ipv4
if (useIPv4) {
if (isIPv4)
MainActivity.myInetIP = sAddr;
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
MainActivity.myInetIP =
(delim < 0) ? sAddr.toUpperCase() : sAddr.substring(
0, delim).toUpperCase();
}
}
}
}
// get the mac address // get the mac address
byte[] mac = network.getHardwareAddress(); byte[] mac = network.getHardwareAddress();
@ -118,12 +107,38 @@ public class Utilities {
sb.append(String.format("%02X%s", mac[i], sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? ":" : "")); (i < mac.length - 1) ? ":" : ""));
} }
MainActivity.myMAC = sb.toString(); myMAC = sb.toString();
} }
Log.d(TAG, "myMAC: " + myMAC);
// get the ip address
inetAddresses = network.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String sAddr = inetAddress.getHostAddress();
boolean isIPv4 = sAddr.indexOf(':') < 0;
// check if we only want ipv4
if (useIPv4) {
if (isIPv4)
myInetIP = sAddr;
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
myInetIP =
(delim < 0) ? sAddr.toUpperCase() : sAddr.substring(
0, delim).toUpperCase();
}
}
}
}
Log.d(TAG, "myIP: " + myInetIP);
} }
} catch (SocketException e) { } catch (SocketException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
@ -145,37 +160,34 @@ public class Utilities {
*/ */
protected static int getNumCores() { protected static int getNumCores() {
Process proc; Process proc;
BufferedReader stdout_buf;
String stdout;
try { try {
proc = Runtime.getRuntime().exec("grep -c processor /proc/cpuinfo"); proc = Runtime.getRuntime().exec("grep -c processor /proc/cpuinfo");
InputStream stdout = proc.getInputStream();
byte[] buff = new byte[20];
int read;
StringBuilder out = new StringBuilder();
while(true){
read = stdout.read(buff);
if(read<0){
MainActivity.myHandler.post(new Runnable() {
@Override
public void run() {
MainActivity.txt_results.setText("Failed to get cores\n");
}
});
return 1;
}
out.append(new String(buff, 0, read));
if(read<20){
break;
}
}
proc.waitFor(); proc.waitFor();
stdout.close();
//Return the number of cores (virtual CPU devices) // read std out
return Integer.parseInt(out.toString().trim()); stdout_buf = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
stdout = stdout_buf.readLine();
Log.d(TAG, "Number of cores: " + stdout);
stdout_buf.close();
if (stdout == null) {
Log.w(TAG, "cannot fetch number of cores!");
return 1;
} else {
return Integer.parseInt(stdout);
}
} catch(Exception e) { } catch(Exception e) {
Log.w(TAG, "cannot fetch number of cores!");
MainActivity.myHandler.post(new Runnable() { MainActivity.myHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
MainActivity.txt_results.setText("Failed to get cores\n"); MainActivity.txt_results.append("Failed to get # of cores\n");
} }
}); });
//Default to return 1 core //Default to return 1 core