/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.tez.common.counters;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableUtils;
import org.apache.hadoop.util.StringInterner;

/**
 * A generic counter implementation
 */
@InterfaceAudience.Private
public class GenericCounter extends AbstractCounter {

  private String name;
  private String displayName;
  private long value = 0;

  public GenericCounter() {
    // mostly for readFields
  }

  public GenericCounter(String name, String displayName) {
    this(name, displayName, 0);
  }

  public GenericCounter(String name, String displayName, long value) {
    this.name = StringInterner.weakIntern(name);
    this.displayName = StringInterner.weakIntern(displayName);
    this.value = value;
  }

  @Override @Deprecated
  public synchronized void setDisplayName(String displayName) {
    this.displayName = displayName;
  }

  @Override
  public synchronized void readFields(DataInput in) throws IOException {
    name = StringInterner.weakIntern(Text.readString(in));
    displayName = in.readBoolean() ? StringInterner.weakIntern(Text.readString(in)) : name;
    value = WritableUtils.readVLong(in);
  }

  /**
   * GenericCounter ::= keyName isDistinctDisplayName [displayName] value
   */
  @Override
  public synchronized void write(DataOutput out) throws IOException {
    Text.writeString(out, name);
    boolean distinctDisplayName = ! name.equals(displayName);
    out.writeBoolean(distinctDisplayName);
    if (distinctDisplayName) {
      Text.writeString(out, displayName);
    }
    WritableUtils.writeVLong(out, value);
  }

  @Override
  public synchronized String getName() {
    return name;
  }

  @Override
  public synchronized String getDisplayName() {
    return displayName;
  }

  @Override
  public synchronized long getValue() {
    return value;
  }

  @Override
  public synchronized void setValue(long value) {
    this.value = value;
  }

  @Override
  public synchronized void increment(long incr) {
    value += incr;
  }

  @Override
  public TezCounter getUnderlyingCounter() {
    return this;
  }
}

@@ -921,16 +921,63 @@
 ception;
+%0Aimport java.util.concurrent.atomic.AtomicLong;
 %0A%0Aimport
@@ -1346,22 +1346,50 @@
 ate 
-long value = 0
+final AtomicLong value = new AtomicLong(0)
 ;%0A%0A 
@@ -1738,32 +1738,35 @@
   this.value
- = 
+.set(
 value
+)
 ;%0A  %7D%0A%0A  @Ov
@@ -2134,19 +2134,21 @@
    value
- = 
+.set(
 Writable
@@ -2166,16 +2166,17 @@
 Long(in)
+)
 ;%0A  %7D%0A%0A 
@@ -2594,24 +2594,30 @@
 g(out, value
+.get()
 );%0A  %7D%0A%0A  @O
@@ -2797,29 +2797,16 @@
  public 
-synchronized 
 long get
@@ -2827,24 +2827,30 @@
 return value
+.get()
 ;%0A  %7D%0A%0A  @Ov
@@ -2861,37 +2861,24 @@
 de%0A  public 
-synchronized 
 void setValu
@@ -2911,16 +2911,19 @@
 alue
- = 
+.set(
 value
+)
 ;%0A  
@@ -2942,37 +2942,24 @@
 de%0A  public 
-synchronized 
 void increme
@@ -2987,16 +2987,24 @@
 alue
- += 
+.addAndGet(
 incr
+)
 ;%0A  

Fix Tez GenericCounter performance bottleneck (Gopal V, reviewed by Rajesh Balamohan)