Java Code Examples for com.google.protobuf.CodedOutputStream#writeInt64()

The following examples show how to use com.google.protobuf.CodedOutputStream#writeInt64() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CpuProfiler.java    From bazel with Apache License 2.0 6 votes vote down vote up
synchronized void writeEvent(int ticks, ImmutableList<Debug.Frame> stack) {
  if (this.error == null) {
    try {
      ByteArrayOutputStream sample = new ByteArrayOutputStream();
      CodedOutputStream sampleEnc = CodedOutputStream.newInstance(sample);
      sampleEnc.writeInt64(SAMPLE_VALUE, ticks * period.toNanos() / 1000L);
      for (Debug.Frame fr : stack.reverse()) {
        sampleEnc.writeUInt64(SAMPLE_LOCATION_ID, getLocationID(fr));
      }
      sampleEnc.flush();
      enc.writeByteArray(PROFILE_SAMPLE, sample.toByteArray());
    } catch (IOException ex) {
      this.error = ex;
    }
  }
}
 
Example 2
Source File: CpuProfiler.java    From bazel with Apache License 2.0 5 votes vote down vote up
PprofWriter(OutputStream out, Duration period) {
  this.period = period;
  this.startNano = System.nanoTime();

  try {
    this.gz = new GZIPOutputStream(out);
    this.enc = CodedOutputStream.newInstance(gz);
    getStringID(""); // entry 0 is always ""

    // dimension and unit
    ByteArrayOutputStream unit = new ByteArrayOutputStream();
    CodedOutputStream unitEnc = CodedOutputStream.newInstance(unit);
    unitEnc.writeInt64(VALUETYPE_TYPE, getStringID("CPU"));
    unitEnc.writeInt64(VALUETYPE_UNIT, getStringID("microseconds"));
    unitEnc.flush();

    // informational fields of Profile
    enc.writeByteArray(PROFILE_SAMPLE_TYPE, unit.toByteArray());
    // magnitude of sampling period:
    enc.writeInt64(PROFILE_PERIOD, period.toNanos() / 1000L);
    // dimension and unit of period:
    enc.writeByteArray(PROFILE_PERIOD_TYPE, unit.toByteArray());
    // start (real) time of profile:
    enc.writeInt64(PROFILE_TIME_NANOS, System.currentTimeMillis() * 1000000L);
  } catch (IOException ex) {
    this.error = ex;
  }
}
 
Example 3
Source File: CpuProfiler.java    From bazel with Apache License 2.0 5 votes vote down vote up
private long getFunctionID(StarlarkCallable fn, long addr) throws IOException {
  Long id = functionIDs.get(addr);
  if (id == null) {
    id = addr;

    Location loc = fn.getLocation();
    String filename = loc.file(); // TODO(adonovan): make WORKSPACE-relative
    String name = fn.getName();
    if (name.equals("<toplevel>")) {
      name = filename;
    }

    long nameID = getStringID(name);

    ByteArrayOutputStream fun = new ByteArrayOutputStream();
    CodedOutputStream funEnc = CodedOutputStream.newInstance(fun);
    funEnc.writeUInt64(FUNCTION_ID, id);
    funEnc.writeInt64(FUNCTION_NAME, nameID);
    funEnc.writeInt64(FUNCTION_SYSTEM_NAME, nameID);
    funEnc.writeInt64(FUNCTION_FILENAME, getStringID(filename));
    funEnc.writeInt64(FUNCTION_START_LINE, (long) loc.line());
    funEnc.flush();
    enc.writeByteArray(PROFILE_FUNCTION, fun.toByteArray());

    functionIDs.put(addr, id);
  }
  return id;
}
 
Example 4
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
private void writeTo(FieldInfo fieldInfo, Object value, CodedOutputStream out) throws IOException {
	FieldType fieldType = fieldInfo.getFieldType();
	int order = fieldInfo.getOrder();

	if (value instanceof List) {
		// if check list
		CodedConstant.writeToList(out, order, fieldType, (List) value);
		return;
	}

	switch (fieldType) {
	case DOUBLE:
		out.writeDouble(order, (Double) value);
		break;
	case BYTES:
		ByteString bytes = ByteString.copyFrom((byte[]) value);
		out.writeBytes(order, bytes);
		break;
	case STRING:
		ByteString string = ByteString.copyFromUtf8(value.toString());
		out.writeBytes(order, string);
		break;
	case BOOL:
		out.writeBool(order, (Boolean) value);
		break;
	case FIXED32:
		out.writeFixed32(order, (Integer) value);
		break;
	case SFIXED32:
		out.writeSFixed32(order, (Integer) value);
		break;
	case SINT32:
		out.writeSInt32(order, (Integer) value);
		break;
	case INT32:
		out.writeInt32(order, (Integer) value);
		break;
	case UINT32:
		out.writeUInt32(order, (Integer) value);
		break;
	case FIXED64:
		out.writeFixed64(order, (Long) value);
		break;
	case SFIXED64:
		out.writeSFixed64(order, (Long) value);
		break;
	case SINT64:
		out.writeSInt64(order, (Long) value);
		break;
	case INT64:
		out.writeInt64(order, (Long) value);
		break;
	case UINT64:
		out.writeUInt64(order, (Long) value);
		break;
	case ENUM:
		int i;
		i = getEnumValue(value);
		out.writeEnum(order, i);
		break;
	case FLOAT:
		out.writeFloat(order, (Float) value);
		break;
	case OBJECT:
		Class c = value.getClass();
		ReflectiveCodec codec = new ReflectiveCodec(c);
		out.writeRawVarint32(CodedConstant.makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
		out.writeRawVarint32(codec.size(value));
		codec.writeTo(value, out);
		break;
	default:
		throw new IOException("Unknown field type on field '" + fieldInfo.getField().getName() + "'");
	}

}
 
Example 5
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Write object to byte array by {@link FieldType}
 * 
 * @param out
 * @param order
 * @param type
 * @param o
 * @throws IOException
 */
public static void writeObject(CodedOutputStream out, int order, FieldType type, Object o, boolean list)
        throws IOException {
    if (o == null) {
        return;
    }

    if (type == FieldType.OBJECT) {

        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls);

        out.writeRawVarint32(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
        out.writeRawVarint32(target.size(o));

        target.writeTo(o, out);
        return;
    }

    if (type == FieldType.BOOL) {
        out.writeBool(order, (Boolean) o);
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        out.writeBytes(order, ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        out.writeDouble(order, (Double) o);
    } else if (type == FieldType.FIXED32) {
        out.writeFixed32(order, (Integer) o);
    } else if (type == FieldType.FIXED64) {
        out.writeFixed64(order, (Long) o);
    } else if (type == FieldType.FLOAT) {
        out.writeFloat(order, (Float) o);
    } else if (type == FieldType.INT32) {
        out.writeInt32(order, (Integer) o);
    } else if (type == FieldType.INT64) {
        out.writeInt64(order, (Long) o);
    } else if (type == FieldType.SFIXED32) {
        out.writeSFixed32(order, (Integer) o);
    } else if (type == FieldType.SFIXED64) {
        out.writeSFixed64(order, (Long) o);
    } else if (type == FieldType.SINT32) {
        out.writeSInt32(order, (Integer) o);
    } else if (type == FieldType.SINT64) {
        out.writeSInt64(order, (Long) o);
    } else if (type == FieldType.STRING) {
        out.writeBytes(order, ByteString.copyFromUtf8(String.valueOf(o)));
    } else if (type == FieldType.UINT32) {
        out.writeUInt32(order, (Integer) o);
    } else if (type == FieldType.UINT64) {
        out.writeUInt64(order, (Long) o);
    } else if (type == FieldType.ENUM) {
        int value = 0;
        if (o instanceof EnumReadable) {
            value = ((EnumReadable) o).value();
        } else if (o instanceof Enum) {
            value = ((Enum) o).ordinal();
        }
        out.writeEnum(order, value);
    }
}
 
Example 6
Source File: CpuProfiler.java    From bazel with Apache License 2.0 4 votes vote down vote up
private long getLocationID(Debug.Frame fr) throws IOException {
  StarlarkCallable fn = fr.getFunction();
  // fnAddr identifies a function as a whole.
  int fnAddr = System.identityHashCode(fn); // very imperfect

  // pcAddr identifies the current program point.
  //
  // For now, this is the same as fnAddr, because
  // we don't track the syntax node currently being
  // evaluated. Statement-level profile information
  // in the leaf function (displayed by 'pprof list <fn>')
  // is thus unreliable for now.
  long pcAddr = fnAddr;
  if (fn instanceof StarlarkFunction) {
    // TODO(adonovan): when we use a byte code representation
    // of function bodies, mix the program counter fr.pc into fnAddr.
    // TODO(adonovan): even cleaner: treat each function's byte
    // code segment as its own Profile.Mapping, indexed by pc.
    //
    // pcAddr = (pcAddr << 16) ^ fr.pc;
  }

  Long id = locationIDs.get(pcAddr);
  if (id == null) {
    id = pcAddr;

    ByteArrayOutputStream line = new ByteArrayOutputStream();
    CodedOutputStream lineenc = CodedOutputStream.newInstance(line);
    lineenc.writeUInt64(LINE_FUNCTION_ID, getFunctionID(fn, fnAddr));
    lineenc.writeInt64(LINE_LINE, (long) fr.getLocation().line());
    lineenc.flush();

    ByteArrayOutputStream loc = new ByteArrayOutputStream();
    CodedOutputStream locenc = CodedOutputStream.newInstance(loc);
    locenc.writeUInt64(LOCATION_ID, id);
    locenc.writeUInt64(LOCATION_ADDRESS, pcAddr);
    locenc.writeByteArray(LOCATION_LINE, line.toByteArray());
    locenc.flush();
    enc.writeByteArray(PROFILE_LOCATION, loc.toByteArray());

    locationIDs.put(pcAddr, id);
  }
  return id;
}