Browse Source

Added experimental auto buffer calculation in Form Post Upload

TC pushbot 5 4 years ago
parent
commit
4824159f17
3 changed files with 124 additions and 21 deletions
  1. 14 2
      file_system.go
  2. 110 1
      mod/info/usageinfo/usageinfo.go
  3. 0 18
      system.info.go

+ 14 - 2
file_system.go

@@ -27,6 +27,7 @@ import (
 	fsp "imuslab.com/arozos/mod/filesystem/fspermission"
 	hidden "imuslab.com/arozos/mod/filesystem/hidden"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
+	usage "imuslab.com/arozos/mod/info/usageinfo"
 	module "imuslab.com/arozos/mod/modules"
 	prout "imuslab.com/arozos/mod/prouter"
 	"imuslab.com/arozos/mod/share"
@@ -501,7 +502,7 @@ func system_fs_handleLowMemoryUpload(w http.ResponseWriter, r *http.Request) {
 /*
 	Handle FORM POST based upload
 
-	This function is design for general SBCs or computers with more than 1GB of RAM
+	This function is design for general SBCs or computers with more than 2GB of RAM
 	(e.g. Raspberry Pi 4 / Linux Server)
 */
 func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
@@ -524,7 +525,18 @@ func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	err = r.ParseMultipartForm(int64(*upload_buf) << 20)
+	//Experimental code: Get the remaining memory and use 1/2 - 10MB of it
+	//See https://golang.org/pkg/mime/multipart/#Reader.ReadForm for why 10MB
+	usedRam, totalRam := usage.GetNumericRAMUsage()
+	if usedRam > 0 && totalRam > 0 {
+		autoCalculatedUploadBuf := ((totalRam-usedRam)/2)/1048576 - 10
+		log.Println("*Testing* Upload buffer set to ", autoCalculatedUploadBuf, "MB")
+		err = r.ParseMultipartForm(int64(autoCalculatedUploadBuf) << 20)
+	} else {
+		//Unable to get RAM usage. Use default parser
+		err = r.ParseMultipartForm(int64(*upload_buf) << 20)
+	}
+
 	if err != nil {
 		//Filesize too big
 		log.Println(err)

+ 110 - 1
mod/info/usageinfo/usageinfo.go

@@ -36,7 +36,6 @@ func GetCPUUsage() float64 {
 		}
 		usage = s
 	} else if runtime.GOOS == "linux" || runtime.GOOS == "freebsd" {
-
 		//Get CPU first 10 processes uses most CPU resources
 		cmd := exec.Command("bash", "-c", query_cpuproc_command)
 		out, err := cmd.CombinedOutput()
@@ -91,6 +90,116 @@ func GetCPUUsage() float64 {
 	return usage
 }
 
+//Get RAM Usage in Numeric values
+func GetNumericRAMUsage() (int64, int64) {
+	usedRam := int64(-1)
+	totalRam := int64(-1)
+	if runtime.GOOS == "windows" {
+		cmd := exec.Command("system/hardware/windows/RAMUsage.exe")
+		out, err := cmd.CombinedOutput()
+		if err != nil {
+			return -1, -1
+		}
+		raminfo := strings.Split(strings.TrimSpace(string(out)), ",")
+		if len(raminfo) == 3 {
+
+			//The returned value is something like this
+			//7639 MB,16315 MB,0.468219429972418
+			tmp := strings.Split(raminfo[0], " ")[0]
+			used, err := strconv.ParseInt(tmp, 10, 64)
+			if err != nil {
+				return -1, -1
+			}
+
+			tmp = strings.Split(raminfo[1], " ")[0]
+			total, err := strconv.ParseInt(tmp, 10, 64)
+			if err != nil {
+				return -1, -1
+			}
+
+			usedRam = used * 1024 * 1024   //From MB to Bytes
+			totalRam = total * 1024 * 1024 //From MB to Bytes
+
+			return usedRam, totalRam
+		} else {
+			return -1, -1
+		}
+
+	} else if runtime.GOOS == "linux" {
+		cmd := exec.Command("bash", "-c", "free -m | grep Mem:")
+		out, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+
+		//If the output contain more than one Memory info, only use the first one
+		if strings.Contains(string(out), "\n") {
+			out = []byte(strings.Split(string(out), "\n")[0])
+		}
+
+		//Trim of double space to space
+		for strings.Contains(string(out), "  ") {
+			out = []byte(strings.ReplaceAll(string(out), "  ", " "))
+		}
+
+		data := strings.Split(string(out), " ")
+		if len(data) > 3 {
+			used, err := strconv.ParseInt(data[2], 10, 64)
+			if err != nil {
+				return -1, -1
+			}
+
+			total, err := strconv.ParseInt(data[1], 10, 64)
+			if err != nil {
+				return -1, -1
+			}
+
+			usedRam = used * 1024 * 1024
+			totalRam = total * 1024 * 1024
+
+			return usedRam, totalRam
+		}
+
+	} else if runtime.GOOS == "freebsd" {
+
+		// Get usused memory size (free)
+		cmd := exec.Command("bash", "-c", query_freemem_command)
+		freeMemByteArr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+		freeMemStr := string(freeMemByteArr)
+		freeMemStr = strings.ReplaceAll(freeMemStr, "\n", "")
+		freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
+
+		// Get phy memory size
+		cmd = exec.Command("bash", "-c", query_phymem_command)
+		phyMemByteArr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+
+		phyMemStr := string(phyMemByteArr)
+		phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
+
+		// phyMemSize in MB
+		phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
+		phyMemSizeFloat = math.Floor(phyMemSizeFloat)
+		total := phyMemSizeFloat
+
+		// Used memory
+		usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
+		usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
+		used := usedRAMSizeFloat
+
+		totalRam = int64(total)
+		usedRam = int64(used)
+
+		return usedRam, totalRam
+	}
+	return -1, -1
+}
+
 //Get RAM usage, return used / total / used percentage
 func GetRAMUsage() (string, string, float64) {
 	usedRam := "Unknown"

+ 0 - 18
system.info.go

@@ -37,24 +37,6 @@ func SystemInfoInit() {
 			CPUArch:      runtime.GOARCH,
 			HostName:     *host_name,
 		})
-		/*
-			if runtime.GOOS == "windows" {
-				//this features only working on windows, so display on win at now
-				http.HandleFunc("/system/info/getCPUinfo", getCPUinfo)
-				http.HandleFunc("/system/info/ifconfig", ifconfig)
-				http.HandleFunc("/system/info/getDriveStat", getDriveStat)
-				http.HandleFunc("/system/info/usbPorts", getUSB)
-				http.HandleFunc("/system/info/getRAMinfo", getRAMinfo)
-
-			} else if runtime.GOOS == "linux" {
-				//this features only working on windows, so display on win at now
-				http.HandleFunc("/system/info/getCPUinfo", getCPUinfoLinux)
-				http.HandleFunc("/system/info/ifconfig", ifconfigLinux)
-				http.HandleFunc("/system/info/getDriveStat", getDriveStatLinux)
-				http.HandleFunc("/system/info/usbPorts", getUSBLinux)
-				http.HandleFunc("/system/info/getRAMinfo", getRAMinfoLinux)
-			}
-		*/
 
 		router.HandleFunc("/system/info/getCPUinfo", info.GetCPUInfo)
 		router.HandleFunc("/system/info/ifconfig", info.Ifconfig)