github.com/Cloud-Foundations/Dominator@v0.3.4/cmd/subd/vCpu.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/Cloud-Foundations/Dominator/lib/constants"
    11  	"github.com/Cloud-Foundations/Dominator/lib/log"
    12  )
    13  
    14  func getAwsInstanceType() (string, error) {
    15  	resp, err := http.Get(constants.MetadataUrl +
    16  		constants.MetadataAwsInstanceType)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	defer resp.Body.Close()
    21  	if resp.StatusCode != 200 {
    22  		return "", errors.New(resp.Status)
    23  	}
    24  	if body, err := ioutil.ReadAll(resp.Body); err != nil {
    25  		return "", err
    26  	} else {
    27  		value := strings.TrimSpace(string(body))
    28  		if value == "" {
    29  			return "", errors.New("empty body")
    30  		} else {
    31  			return value, nil
    32  		}
    33  	}
    34  }
    35  
    36  func getAwsVcpuCreditRate() (uint, error) {
    37  	iType, err := getAwsInstanceType()
    38  	if err != nil {
    39  		return 0, err
    40  	}
    41  	switch iType {
    42  	case "t2.nano":
    43  		return 3, nil
    44  	case "t2.micro":
    45  		return 6, nil
    46  	case "t2.small":
    47  		return 12, nil
    48  	case "t2.medium":
    49  		return 24, nil
    50  	case "t2.large":
    51  		return 36, nil
    52  	case "t2.xlarge":
    53  		return 54, nil
    54  	case "t2.2xlarge":
    55  		return 81, nil
    56  	default:
    57  		return 0, nil
    58  	}
    59  }
    60  
    61  func adjustVcpuLimit(limit *uint, logger log.Logger) {
    62  	initialLimit := *limit
    63  	vCpuCreditRate, err := getAwsVcpuCreditRate()
    64  	if err == nil && vCpuCreditRate > 0 {
    65  		newLimit := initialLimit * vCpuCreditRate / 60 / uint(runtime.NumCPU())
    66  		logger.Printf("Adjusting default CPU limit to: %d%%\n", newLimit)
    67  		*limit = newLimit
    68  	}
    69  }