View Javadoc

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  package org.apache.giraph.partition;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  
24  import org.apache.hadoop.io.Writable;
25  
26  /**
27   * Used to keep track of statistics of every {@link Partition}. Contains no
28   * actual partition data, only the statistics.
29   */
30  public class PartitionStats implements Writable {
31    /** Id of partition to keep stats for */
32    private int partitionId = -1;
33    /** Vertices in this partition */
34    private long vertexCount = 0;
35    /** Finished vertices in this partition */
36    private long finishedVertexCount = 0;
37    /** Edges in this partition */
38    private long edgeCount = 0;
39    /** Messages sent from this partition */
40    private long messagesSentCount = 0;
41    /** Message byetes sent from this partition */
42    private long messageBytesSentCount = 0;
43  
44    /**
45     * Default constructor for reflection.
46     */
47    public PartitionStats() { }
48  
49    /**
50     * Constructor with the initial stats.
51     *
52     * @param partitionId Partition count.
53     * @param vertexCount Vertex count.
54     * @param finishedVertexCount Finished vertex count.
55     * @param edgeCount Edge count.
56     * @param messagesSentCount Number of messages sent
57     * @param messageBytesSentCount Number of message bytes sent
58     */
59    public PartitionStats(int partitionId,
60        long vertexCount,
61        long finishedVertexCount,
62        long edgeCount,
63        long messagesSentCount,
64        long messageBytesSentCount) {
65      this.partitionId = partitionId;
66      this.vertexCount = vertexCount;
67      this.finishedVertexCount = finishedVertexCount;
68      this.edgeCount = edgeCount;
69      this.messagesSentCount = messagesSentCount;
70      this.messageBytesSentCount = messageBytesSentCount;
71    }
72  
73    /**
74     * Set the partition id.
75     *
76     * @param partitionId New partition id.
77     */
78    public void setPartitionId(int partitionId) {
79      this.partitionId = partitionId;
80    }
81  
82    /**
83     * Get partition id.
84     *
85     * @return Partition id.
86     */
87    public int getPartitionId() {
88      return partitionId;
89    }
90  
91    /**
92     * Increment the vertex count by one.
93     */
94    public void incrVertexCount() {
95      ++vertexCount;
96    }
97  
98    /**
99     * Get the vertex count.
100    *
101    * @return Vertex count.
102    */
103   public long getVertexCount() {
104     return vertexCount;
105   }
106 
107   /**
108    * Increment the finished vertex count by one.
109    */
110   public void incrFinishedVertexCount() {
111     ++finishedVertexCount;
112   }
113 
114   /**
115    * Get the finished vertex count.
116    *
117    * @return Finished vertex count.
118    */
119   public long getFinishedVertexCount() {
120     return finishedVertexCount;
121   }
122 
123   /**
124    * Add edges to the edge count.
125    *
126    * @param edgeCount Number of edges to add.
127    */
128   public void addEdgeCount(long edgeCount) {
129     this.edgeCount += edgeCount;
130   }
131 
132   /**
133    * Get the edge count.
134    *
135    * @return Edge count.
136    */
137   public long getEdgeCount() {
138     return edgeCount;
139   }
140 
141   /**
142    * Add messages to the messages sent count.
143    *
144    * @param messagesSentCount Number of messages to add.
145    */
146   public void addMessagesSentCount(long messagesSentCount) {
147     this.messagesSentCount += messagesSentCount;
148   }
149 
150   /**
151    * Get the messages sent count.
152    *
153    * @return Messages sent count.
154    */
155   public long getMessagesSentCount() {
156     return messagesSentCount;
157   }
158 
159   /**
160    * Add message bytes to messageBytesSentCount.
161    *
162    * @param messageBytesSentCount Number of message bytes to add.
163    */
164   public void addMessageBytesSentCount(long messageBytesSentCount) {
165     this.messageBytesSentCount += messageBytesSentCount;
166   }
167 
168   /**
169    * Get the message bytes sent count.
170    *
171    * @return Message bytes sent count.
172    */
173   public long getMessageBytesSentCount() {
174     return messageBytesSentCount;
175   }
176 
177   @Override
178   public void readFields(DataInput input) throws IOException {
179     partitionId = input.readInt();
180     vertexCount = input.readLong();
181     finishedVertexCount = input.readLong();
182     edgeCount = input.readLong();
183     messagesSentCount = input.readLong();
184     messageBytesSentCount = input.readLong();
185   }
186 
187   @Override
188   public void write(DataOutput output) throws IOException {
189     output.writeInt(partitionId);
190     output.writeLong(vertexCount);
191     output.writeLong(finishedVertexCount);
192     output.writeLong(edgeCount);
193     output.writeLong(messagesSentCount);
194     output.writeLong(messageBytesSentCount);
195   }
196 
197   @Override
198   public String toString() {
199     return "(id=" + partitionId + ",vtx=" + vertexCount + ",finVtx=" +
200         finishedVertexCount + ",edges=" + edgeCount + ",msgsSent=" +
201         messagesSentCount + ",msgBytesSent=" +
202           messageBytesSentCount + ")";
203   }
204 }