// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package procfs

import (
	
	
	
	
	
	

	
)

// Softirqs represents the softirq statistics.
type Softirqs struct {
	Hi      []uint64
	Timer   []uint64
	NetTx   []uint64
	NetRx   []uint64
	Block   []uint64
	IRQPoll []uint64
	Tasklet []uint64
	Sched   []uint64
	HRTimer []uint64
	RCU     []uint64
}

func ( FS) () (Softirqs, error) {
	 := .proc.Path("softirqs")
	,  := util.ReadFileNoStat()
	if  != nil {
		return Softirqs{}, 
	}

	 := bytes.NewReader()

	return parseSoftirqs()
}

func parseSoftirqs( io.Reader) (Softirqs, error) {
	var (
		 = Softirqs{}
		  = bufio.NewScanner()
	)

	if !.Scan() {
		return Softirqs{}, fmt.Errorf("%w: softirqs empty", ErrFileRead)
	}

	for .Scan() {
		 := strings.Fields(.Text())
		var  error

		// require at least one cpu
		if len() < 2 {
			continue
		}
		switch [0] {
		case "HI:":
			 := [1:]
			.Hi = make([]uint64, len())
			for ,  := range  {
				if .Hi[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (HI%d): %w", ErrFileParse, , , )
				}
			}
		case "TIMER:":
			 := [1:]
			.Timer = make([]uint64, len())
			for ,  := range  {
				if .Timer[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (TIMER%d): %w", ErrFileParse, , , )
				}
			}
		case "NET_TX:":
			 := [1:]
			.NetTx = make([]uint64, len())
			for ,  := range  {
				if .NetTx[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (NET_TX%d): %w", ErrFileParse, , , )
				}
			}
		case "NET_RX:":
			 := [1:]
			.NetRx = make([]uint64, len())
			for ,  := range  {
				if .NetRx[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (NET_RX%d): %w", ErrFileParse, , , )
				}
			}
		case "BLOCK:":
			 := [1:]
			.Block = make([]uint64, len())
			for ,  := range  {
				if .Block[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (BLOCK%d): %w", ErrFileParse, , , )
				}
			}
		case "IRQ_POLL:":
			 := [1:]
			.IRQPoll = make([]uint64, len())
			for ,  := range  {
				if .IRQPoll[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (IRQ_POLL%d): %w", ErrFileParse, , , )
				}
			}
		case "TASKLET:":
			 := [1:]
			.Tasklet = make([]uint64, len())
			for ,  := range  {
				if .Tasklet[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (TASKLET%d): %w", ErrFileParse, , , )
				}
			}
		case "SCHED:":
			 := [1:]
			.Sched = make([]uint64, len())
			for ,  := range  {
				if .Sched[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (SCHED%d): %w", ErrFileParse, , , )
				}
			}
		case "HRTIMER:":
			 := [1:]
			.HRTimer = make([]uint64, len())
			for ,  := range  {
				if .HRTimer[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (HRTIMER%d): %w", ErrFileParse, , , )
				}
			}
		case "RCU:":
			 := [1:]
			.RCU = make([]uint64, len())
			for ,  := range  {
				if .RCU[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Softirqs{}, fmt.Errorf("%w: couldn't parse %q (RCU%d): %w", ErrFileParse, , , )
				}
			}
		}
	}

	if  := .Err();  != nil {
		return Softirqs{}, fmt.Errorf("%w: couldn't parse softirqs: %w", ErrFileParse, )
	}

	return , .Err()
}