1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair;
20 
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.metrics2.MetricsSystem;
23 import org.apache.hadoop.metrics2.annotation.Metric;
24 import org.apache.hadoop.metrics2.annotation.Metrics;
25 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
26 import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
27 import org.apache.hadoop.yarn.api.records.Resource;
28 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.Queue;
29 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.QueueMetrics;
30 
31 @Metrics(context="yarn")
32 public class FSQueueMetrics extends QueueMetrics {
33 
34   @Metric("Fair share of memory in MB") MutableGaugeInt fairShareMB;
35   @Metric("Fair share of CPU in vcores") MutableGaugeInt fairShareVCores;
36   @Metric("Steady fair share of memory in MB") MutableGaugeInt steadyFairShareMB;
37   @Metric("Steady fair share of CPU in vcores") MutableGaugeInt steadyFairShareVCores;
38   @Metric("Minimum share of memory in MB") MutableGaugeInt minShareMB;
39   @Metric("Minimum share of CPU in vcores") MutableGaugeInt minShareVCores;
40   @Metric("Maximum share of memory in MB") MutableGaugeInt maxShareMB;
41   @Metric("Maximum share of CPU in vcores") MutableGaugeInt maxShareVCores;
42 
FSQueueMetrics(MetricsSystem ms, String queueName, Queue parent, boolean enableUserMetrics, Configuration conf)43   FSQueueMetrics(MetricsSystem ms, String queueName, Queue parent,
44       boolean enableUserMetrics, Configuration conf) {
45     super(ms, queueName, parent, enableUserMetrics, conf);
46   }
47 
setFairShare(Resource resource)48   public void setFairShare(Resource resource) {
49     fairShareMB.set(resource.getMemory());
50     fairShareVCores.set(resource.getVirtualCores());
51   }
52 
getFairShareMB()53   public int getFairShareMB() {
54     return fairShareMB.value();
55   }
56 
getFairShareVirtualCores()57   public int getFairShareVirtualCores() {
58     return fairShareVCores.value();
59   }
60 
setSteadyFairShare(Resource resource)61   public void setSteadyFairShare(Resource resource) {
62     steadyFairShareMB.set(resource.getMemory());
63     steadyFairShareVCores.set(resource.getVirtualCores());
64   }
65 
getSteadyFairShareMB()66   public int getSteadyFairShareMB() {
67     return steadyFairShareMB.value();
68   }
69 
getSteadyFairShareVCores()70   public int getSteadyFairShareVCores() {
71     return steadyFairShareVCores.value();
72   }
73 
setMinShare(Resource resource)74   public void setMinShare(Resource resource) {
75     minShareMB.set(resource.getMemory());
76     minShareVCores.set(resource.getVirtualCores());
77   }
78 
getMinShareMB()79   public int getMinShareMB() {
80     return minShareMB.value();
81   }
82 
getMinShareVirtualCores()83   public int getMinShareVirtualCores() {
84     return minShareVCores.value();
85   }
86 
setMaxShare(Resource resource)87   public void setMaxShare(Resource resource) {
88     maxShareMB.set(resource.getMemory());
89     maxShareVCores.set(resource.getVirtualCores());
90   }
91 
getMaxShareMB()92   public int getMaxShareMB() {
93     return maxShareMB.value();
94   }
95 
getMaxShareVirtualCores()96   public int getMaxShareVirtualCores() {
97     return maxShareVCores.value();
98   }
99 
100   public synchronized
forQueue(String queueName, Queue parent, boolean enableUserMetrics, Configuration conf)101   static FSQueueMetrics forQueue(String queueName, Queue parent,
102       boolean enableUserMetrics, Configuration conf) {
103     MetricsSystem ms = DefaultMetricsSystem.instance();
104     QueueMetrics metrics = queueMetrics.get(queueName);
105     if (metrics == null) {
106       metrics = new FSQueueMetrics(ms, queueName, parent, enableUserMetrics, conf)
107           .tag(QUEUE_INFO, queueName);
108 
109       // Register with the MetricsSystems
110       if (ms != null) {
111         metrics = ms.register(
112                 sourceName(queueName).toString(),
113                 "Metrics for queue: " + queueName, metrics);
114       }
115       queueMetrics.put(queueName, metrics);
116     }
117 
118     return (FSQueueMetrics)metrics;
119   }
120 
121 }
122