| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | // Copyright 2017 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 04:35:24 -07:00
										 |  |  | //go:build (freebsd || dragonfly) && !noexec
 | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | // +build freebsd dragonfly
 | 
					
						
							|  |  |  | // +build !noexec
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type execCollector struct { | 
					
						
							|  |  |  | 	sysctls []bsdSysctl | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger  *slog.Logger | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 	registerCollector("exec", defaultEnabled, NewExecCollector) | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 13:33:46 -08:00
										 |  |  | // NewExecCollector returns a new Collector exposing system execution statistics.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewExecCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 	// From sys/vm/vm_meter.c:
 | 
					
						
							|  |  |  | 	// All are of type CTLTYPE_UINT.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// vm.stats.sys.v_swtch: Context switches
 | 
					
						
							|  |  |  | 	// vm.stats.sys.v_trap: Traps
 | 
					
						
							|  |  |  | 	// vm.stats.sys.v_syscall: System calls
 | 
					
						
							|  |  |  | 	// vm.stats.sys.v_intr: Device interrupts
 | 
					
						
							|  |  |  | 	// vm.stats.sys.v_soft: Software interrupts
 | 
					
						
							|  |  |  | 	// vm.stats.vm.v_forks: Number of fork() calls
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &execCollector{ | 
					
						
							|  |  |  | 		sysctls: []bsdSysctl{ | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_context_switches_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "Context switches since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.sys.v_swtch", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_traps_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "Traps since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.sys.v_trap", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_system_calls_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "System calls since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.sys.v_syscall", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_device_interrupts_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "Device interrupts since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.sys.v_intr", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_software_interrupts_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "Software interrupts since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.sys.v_soft", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				name:        "exec_forks_total", | 
					
						
							| 
									
										
										
										
											2018-04-09 08:27:30 -07:00
										 |  |  | 				description: "Number of fork() calls since system boot.  Resets at architecture unsigned integer.", | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				mib:         "vm.stats.vm.v_forks", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 		logger: logger, | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Update pushes exec statistics onto ch
 | 
					
						
							| 
									
										
										
										
											2017-02-28 13:33:46 -08:00
										 |  |  | func (c *execCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 	for _, m := range c.sysctls { | 
					
						
							|  |  |  | 		v, err := m.Value() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-03-08 03:59:48 -08:00
										 |  |  | 				namespace+"_"+m.name, | 
					
						
							| 
									
										
										
										
											2017-02-28 13:23:10 -08:00
										 |  |  | 				m.description, | 
					
						
							|  |  |  | 				nil, nil, | 
					
						
							|  |  |  | 			), prometheus.CounterValue, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |