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.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity {
// unchanged stuff
@ -69,8 +70,6 @@ public class MainActivity extends Activity {
protected static int currentBandwidth = -1; // bps, default is -1, indicating unlimited
protected static TextView txt_results;
protected static Handler myHandler;
protected static String myInetIP = "";
protected static String myMAC = "";
protected static String RXportNum = "4444";
protected static String outFolderPath;
protected static String btn_click_time;
@ -486,9 +485,11 @@ public class MainActivity extends Activity {
+ existedItems[selectedItems.get(i)] + "_"
+ (bytes2send / 1024) + "KB_"
+ repeatCounts + "repeats_thrpt_"
+ (currentBandwidth / 1000000.0) + "MBps_"
+ new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date())
+ (currentBandwidth == -1 ? "Unlimited" :
(currentBandwidth / 1000000.0) + "MBps_")
+ (new SimpleDateFormat(
"yyyyMMdd_HHmmss", Locale.US)
.format(new Date()))
+ ".tar.gz";
commd[2] = "cd " + outFolderPath + "/"
+ existedItems[selectedItems.get(i)]
@ -600,11 +601,14 @@ public class MainActivity extends Activity {
binary_RX_RawNormal = "client_recv_bypassl3";
// get number of cores
coreNum = Utilities.getNumCores();
// output folder for SSLogger
outFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SSLogger";
if (!Utilities.dirExist(outFolderPath, true)) {
// checked and cannot create this folder
Toast.makeText(this, "Cannot create folder!!!", Toast.LENGTH_LONG).show();
}
// elements in the page
txt_results = (TextView) findViewById(R.id.txt_results);
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_setLogFreq = (Button) findViewById(R.id.btn_setLogFreq);
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));
// click listener
btn_startTransmit.setOnClickListener(new View.OnClickListener() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,9 +27,15 @@ import java.util.List;
/**
* Created by yanzi on 10/1/15.
* Updated by yanzi on 01/27/2017
*/
public class Utilities {
private static final String TAG = "Utilities";
// variables
protected static String myInetIP = "127.0.0.1";
protected static String myMAC = "";
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
@ -75,39 +81,22 @@ public class Utilities {
* get the ip and mac addresses
*/
protected static void getSelfIdentity(String interface_name, boolean useIPv4) {
String name;
Enumeration<NetworkInterface> networks;
Enumeration<InetAddress> inetAddresses;
NetworkInterface network;
try {
Enumeration<NetworkInterface> networks =
NetworkInterface.getNetworkInterfaces();
networks = NetworkInterface.getNetworkInterfaces();
while (networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement();
network = networks.nextElement();
// check if the interface matches the desired one
String name = network.getDisplayName();
name = network.getDisplayName();
if (!name.equals(interface_name))
continue;
// 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();
}
}
}
}
Log.d(TAG, "myInterface: " + interface_name);
// get the mac address
byte[] mac = network.getHardwareAddress();
@ -118,12 +107,38 @@ public class Utilities {
sb.append(String.format("%02X%s", mac[i],
(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) {
e.printStackTrace();
}
}
/**
@ -145,37 +160,34 @@ public class Utilities {
*/
protected static int getNumCores() {
Process proc;
BufferedReader stdout_buf;
String stdout;
try {
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();
stdout.close();
//Return the number of cores (virtual CPU devices)
return Integer.parseInt(out.toString().trim());
// read std out
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) {
Log.w(TAG, "cannot fetch number of cores!");
MainActivity.myHandler.post(new Runnable() {
@Override
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