github.com/neohugo/neohugo@v0.123.8/config/env.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package config
    15  
    16  import (
    17  	"os"
    18  	"runtime"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/pbnjay/memory"
    23  )
    24  
    25  const (
    26  	gigabyte = 1 << 30
    27  )
    28  
    29  // GetNumWorkerMultiplier returns the base value used to calculate the number
    30  // of workers to use for Hugo's parallel execution.
    31  // It returns the value in HUGO_NUMWORKERMULTIPLIER OS env variable if set to a
    32  // positive integer, else the number of logical CPUs.
    33  func GetNumWorkerMultiplier() int {
    34  	if gmp := os.Getenv("HUGO_NUMWORKERMULTIPLIER"); gmp != "" {
    35  		if p, err := strconv.Atoi(gmp); err == nil && p > 0 {
    36  			return p
    37  		}
    38  	}
    39  	return runtime.NumCPU()
    40  }
    41  
    42  // GetMemoryLimit returns the upper memory limit in bytes for Hugo's in-memory caches.
    43  // Note that this does not represent "all of the memory" that Hugo will use,
    44  // so it needs to be set to a lower number than the available system memory.
    45  // It will read from the HUGO_MEMORYLIMIT (in Gigabytes) environment variable.
    46  // If that is not set, it will set aside a quarter of the total system memory.
    47  func GetMemoryLimit() uint64 {
    48  	if mem := os.Getenv("HUGO_MEMORYLIMIT"); mem != "" {
    49  		if v := stringToGibabyte(mem); v > 0 {
    50  			return v
    51  		}
    52  	}
    53  
    54  	// There is a FreeMemory function, but as the kernel in most situations
    55  	// will take whatever memory that is left and use for caching etc.,
    56  	// that value is not something that we can use.
    57  	m := memory.TotalMemory()
    58  	if m != 0 {
    59  		return uint64(m / 4)
    60  	}
    61  
    62  	return 2 * gigabyte
    63  }
    64  
    65  func stringToGibabyte(f string) uint64 {
    66  	if v, err := strconv.ParseFloat(f, 32); err == nil && v > 0 {
    67  		return uint64(v * gigabyte)
    68  	}
    69  	return 0
    70  }
    71  
    72  // SetEnvVars sets vars on the form key=value in the oldVars slice.
    73  func SetEnvVars(oldVars *[]string, keyValues ...string) {
    74  	for i := 0; i < len(keyValues); i += 2 {
    75  		setEnvVar(oldVars, keyValues[i], keyValues[i+1])
    76  	}
    77  }
    78  
    79  func SplitEnvVar(v string) (string, string) {
    80  	name, value, _ := strings.Cut(v, "=")
    81  	return name, value
    82  }
    83  
    84  func setEnvVar(vars *[]string, key, value string) {
    85  	for i := range *vars {
    86  		if strings.HasPrefix((*vars)[i], key+"=") {
    87  			(*vars)[i] = key + "=" + value
    88  			return
    89  		}
    90  	}
    91  	// New var.
    92  	*vars = append(*vars, key+"="+value)
    93  }