...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/time/utils/utils.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/time/utils

     1  // Copyright 2021 Chaos Mesh Authors.
     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  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package utils
    17  
    18  import "github.com/pkg/errors"
    19  
    20  // EncodeClkIds will convert array of clk ids into a mask
    21  func EncodeClkIds(clkIds []string) (uint64, error) {
    22  	mask := uint64(0)
    23  
    24  	for _, id := range clkIds {
    25  		// refer to `uapi/linux/time.h`
    26  		switch id {
    27  		case "CLOCK_REALTIME":
    28  			mask |= 1 << 0
    29  		case "CLOCK_MONOTONIC":
    30  			mask |= 1 << 1
    31  		case "CLOCK_PROCESS_CPUTIME_ID":
    32  			mask |= 1 << 2
    33  		case "CLOCK_THREAD_CPUTIME_ID":
    34  			mask |= 1 << 3
    35  		case "CLOCK_MONOTONIC_RAW":
    36  			mask |= 1 << 4
    37  		case "CLOCK_REALTIME_COARSE":
    38  			mask |= 1 << 5
    39  		case "CLOCK_MONOTONIC_COARSE":
    40  			mask |= 1 << 6
    41  		case "CLOCK_BOOTTIME":
    42  			mask |= 1 << 7
    43  		case "CLOCK_REALTIME_ALARM":
    44  			mask |= 1 << 8
    45  		case "CLOCK_BOOTTIME_ALARM":
    46  			mask |= 1 << 9
    47  		default:
    48  			return 0, errors.Errorf("unknown clock id %s", id)
    49  		}
    50  	}
    51  
    52  	return mask, nil
    53  }
    54